from __future__ import annotations

from datetime import date
from typing import Annotated

from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session

from app.api.deps import CurrentUserContext, get_current_user, get_db
from app.schemas.digital_employee_dashboard import DigitalEmployeeDashboardRead
from app.schemas.finance_dashboard import FinanceDashboardRead
from app.schemas.system_dashboard import SystemDashboardRead
from app.services.digital_employee_dashboard import DigitalEmployeeDashboardService
from app.services.finance_dashboard_access_policy import FinanceDashboardAccessPolicy
from app.services.finance_dashboard_snapshot import FinanceDashboardSnapshotService
from app.services.system_dashboard import SystemDashboardService

router = APIRouter(prefix="/analytics", dependencies=[Depends(get_current_user)])
DbSession = Annotated[Session, Depends(get_db)]
CurrentUser = Annotated[CurrentUserContext, Depends(get_current_user)]


@router.get(
    "/system-dashboard",
    response_model=SystemDashboardRead,
    summary="查询系统看板真实指标",
    description="基于 Agent 运行、工具调用、用户会话和反馈数据聚合系统看板指标。",
)
def get_system_dashboard(
    db: DbSession,
    days: Annotated[
        int,
        Query(ge=1, le=30, description="统计窗口天数。"),
    ] = 7,
) -> SystemDashboardRead:
    return SystemDashboardService(db).build_dashboard(days=days)


@router.get(
    "/digital-employee-dashboard",
    response_model=DigitalEmployeeDashboardRead,
    summary="查询数字员工工作看板",
    description="基于数字员工运行记录和工具调用结果聚合每日工作、技能类型、业务产出和近期执行明细。",
)
def get_digital_employee_dashboard(
    db: DbSession,
    current_user: CurrentUser,
    days: Annotated[
        int,
        Query(ge=1, le=30, description="统计窗口天数。"),
    ] = 7,
    limit: Annotated[
        int,
        Query(ge=1, le=1000, description="窗口内最多读取的运行记录数。"),
    ] = 300,
) -> DigitalEmployeeDashboardRead:
    return DigitalEmployeeDashboardService(
        db,
        tenant_id=current_user.tenant_id,
    ).build_dashboard(days=days, limit=limit)


@router.get(
    "/finance-dashboard",
    response_model=FinanceDashboardRead,
    summary="查询财务看板真实指标",
    description="基于报销单据、风险观察和预算池数据聚合财务看板指标。",
)
def get_finance_dashboard(
    db: DbSession,
    current_user: CurrentUser,
    range_key: Annotated[str, Query(max_length=30, description="顶部时间范围。")] = "近10日",
    start_date: Annotated[date | None, Query(description="自定义开始日期。")] = None,
    end_date: Annotated[date | None, Query(description="自定义结束日期。")] = None,
    trend_range: Annotated[str, Query(max_length=30, description="趋势图时间范围。")] = ("近12天"),
    department_range: Annotated[str, Query(max_length=30, description="排行分析时间范围。")] = (
        "本月"
    ),
) -> FinanceDashboardRead:
    FinanceDashboardAccessPolicy.require_read(current_user)
    return FinanceDashboardSnapshotService(
        db,
        tenant_id=current_user.tenant_id,
    ).build_dashboard(
        range_key=range_key,
        start_date=start_date,
        end_date=end_date,
        trend_range=trend_range,
        department_range=department_range,
    )
