from __future__ import annotations

from fastapi import APIRouter, Request
from fastapi.responses import RedirectResponse

from app.core.db.common import CommonSessionLocal
from app.core.http_responses import ui_access_denied
from app.core.security.csrf import get_or_create_csrf_token
from app.core.security.session_auth import get_current_user
from app.core.templating import templates
from app.modules.core.rbac.deps import get_user_permissions, get_user_roles
from app.modules.staff_dashboard.service import build_staff_dashboard_payload, can_access_staff_dashboard

router = APIRouter(prefix="/staff", tags=["staff-dashboard-v1-ui"])

VALID_TABS = {
    "my-tasks": "modules/staff_dashboard/templates/staff_dashboard/partials/my_tasks.html",
    "due-today": "modules/staff_dashboard/templates/staff_dashboard/partials/due_today.html",
    "overdue": "modules/staff_dashboard/templates/staff_dashboard/partials/overdue.html",
    "client-pending": "modules/staff_dashboard/templates/staff_dashboard/partials/client_pending.html",
    "documents": "modules/staff_dashboard/templates/staff_dashboard/partials/documents.html",
    "returned-work": "modules/staff_dashboard/templates/staff_dashboard/partials/returned_work.html",
    "reports": "modules/staff_dashboard/templates/staff_dashboard/partials/reports.html",
    "wizards": "modules/staff_dashboard/templates/staff_dashboard/partials/wizards.html",
}


def _ctx(request: Request, db, current_user, *, active_tab: str = "my-tasks"):
    payload = build_staff_dashboard_payload(db, request, current_user)
    return {
        "request": request,
        "current_user": current_user,
        "current_user_roles": get_user_roles(db, current_user.id),
        "current_user_permissions": get_user_permissions(db, current_user.id),
        "csrf_token": get_or_create_csrf_token(request),
        "title": "Staff Dashboard",
        "active_tab": active_tab,
        **payload,
    }


@router.get("/dashboard")
def dashboard(request: Request, tab: str = "my-tasks"):
    db = CommonSessionLocal()
    try:
        current_user = get_current_user(request, db=db)
        if not current_user:
            return RedirectResponse(url="/login", status_code=303)
        if not can_access_staff_dashboard(db, current_user):
            return ui_access_denied()
        active_tab = tab if tab in VALID_TABS else "my-tasks"
        return templates.TemplateResponse(
            "modules/staff_dashboard/templates/staff_dashboard/dashboard.html",
            _ctx(request, db, current_user, active_tab=active_tab),
        )
    finally:
        db.close()


@router.get("/dashboard/tab/{tab_name}")
def dashboard_tab(request: Request, tab_name: str):
    db = CommonSessionLocal()
    try:
        current_user = get_current_user(request, db=db)
        if not current_user:
            return RedirectResponse(url="/login", status_code=303)
        if not can_access_staff_dashboard(db, current_user):
            return ui_access_denied()
        active_tab = tab_name if tab_name in VALID_TABS else "my-tasks"
        return templates.TemplateResponse(
            VALID_TABS[active_tab],
            _ctx(request, db, current_user, active_tab=active_tab),
        )
    finally:
        db.close()
