from __future__ import annotations

import sqlite3
from typing import Any, Dict, List

from store_schedule_lib.queries import query_appt_today, query_staff_off_tomorrow


def detect_operational_conflicts(conn: sqlite3.Connection, date: str) -> List[Dict[str, Any]]:
    """经营冲突：明日休息员工仍有今日预约。"""
    conflicts: List[Dict[str, Any]] = []
    off_staff = query_staff_off_tomorrow(conn, date)
    if not off_staff:
        return conflicts
    off_ids = {s["staff_id"] for s in off_staff}
    off_names = {s["staff_id"]: s["name"] for s in off_staff}
    tomorrow = off_staff[0].get("date", "")
    for appt in query_appt_today(conn, date):
        tid = appt.get("technician_id")
        if tid and tid in off_ids:
            conflicts.append({
                "type": "staff_off_with_appt",
                "severity": "warning",
                "message": "员工 {} 明日({})休息，但今日仍有预约 {} {}。".format(
                    off_names.get(tid, tid),
                    tomorrow,
                    appt.get("member_name"),
                    appt.get("start_dt"),
                ),
                "staff_id": tid,
                "appt_id": appt.get("appt_id"),
            })
    return conflicts
