"""
Deterministic routing for manager requests that are out of scope for chat
(FR-07.5): large-team review, bulk approvals, shift planning, and comparative
reporting. The LLM only ever supplies the intent label — the destination URL,
title, and message always come from this trusted, hardcoded config, never
from LLM output. This is the single source of truth for the mapping so no
route is ever hardcoded a second time elsewhere in the graph.
"""

import logging
from typing import Optional, TypedDict

logger = logging.getLogger(__name__)


COMPLEX_MANAGER_INTENTS = {
    "large_team_review",
    "bulk_approvals",
    "shift_planning",
    "comparative_reporting",
}


class NavigationButton(TypedDict):
    label: str
    path: str


class NavigationCard(TypedDict):
    category: str
    title: str
    message: str
    button: NavigationButton


COMPLEX_MANAGER_ROUTE_CONFIG: dict[str, dict] = {
    "large_team_review": {
        "title": "Team Review",
        "message": (
            "Large-team reviews are best handled on the full web dashboard. "
            "Click below to open the Team Review page."
        ),
        "label": "Open Team Review Page",
        "path": "/team/review",
    },
    "bulk_approvals": {
        "title": "Bulk Approvals",
        "message": (
            "Bulk approvals are best handled on the full web dashboard. "
            "Click below to open the Bulk Approvals page."
        ),
        "label": "Open Bulk Approvals Page",
        "path": "/payroll/approvals/bulk",
    },
    "shift_planning": {
        "title": "Shift Planning",
        "message": (
            "Shift planning is best handled on the full web dashboard. "
            "Click below to open the Shift Planning page."
        ),
        "label": "Open Shift Planning Page",
        "path": "/shifts/planner",
    },
    "comparative_reporting": {
        "title": "Comparative Reports",
        "message": (
            "Comparative reporting is best handled on the full web dashboard. "
            "Click below to open the Comparative Reports page."
        ),
        "label": "Open Comparative Reports Page",
        "path": "/reports/comparative",
    },
}


def route_complex_manager_task(intent: str) -> Optional[NavigationCard]:
    """Maps a classified complex-manager intent to its trusted navigation
    card. Returns None for an intent with no configured route — callers must
    fall back to a clarification message rather than inventing a destination."""
    config = COMPLEX_MANAGER_ROUTE_CONFIG.get(intent)
    if not config:
        logger.error("COMPLEX_MANAGER_ROUTE_NOT_CONFIGURED | intent=%s", intent)
        return None

    return {
        "category": intent,
        "title": config["title"],
        "message": config["message"],
        "button": {"label": config["label"], "path": config["path"]},
    }


def log_task_routed(*, user_id, role: str, intent: str, target: str, conversation_id: str) -> None:
    """Best-effort audit trail for a routed complex-manager task. Deliberately
    logging-only (no new DB table) — reuses the existing structured-logging
    convention used across services/erp_client.py and hr_graph.py rather than
    introducing a bespoke audit store for a single event type."""
    logger.info(
        "complex_manager_task_routed | user_id=%s | role=%s | intent=%s | target=%s | conversation_id=%s",
        user_id, role, intent, target, conversation_id,
    )
