"""admin 埋点健康度:埋点成功率 / 上报成功率 总览 + 趋势 + 下钻(只读)。"""
from __future__ import annotations

from datetime import datetime
from typing import Annotated

from fastapi import APIRouter, Depends, Query

from app.admin.deps import AdminDb, require_page
from app.admin.repositories import analytics_health as repo
from app.admin.schemas.analytics_health import (
    HealthBreakdownRow,
    HealthMetrics,
    HealthTrendPoint,
)

router = APIRouter(
    prefix="/admin/api/analytics-health",
    tags=["admin-analytics-health"],
    dependencies=[Depends(require_page("analytics-health"))],
)


@router.get("/overview", response_model=HealthMetrics, summary="两段成功率总览")
def overview(
    db: AdminDb,
    date_from: Annotated[datetime, Query()],
    date_to: Annotated[datetime, Query()],
) -> HealthMetrics:
    return HealthMetrics(**repo.overview(db, date_from, date_to))


@router.get("/trend", response_model=list[HealthTrendPoint], summary="按北京天趋势")
def trend(
    db: AdminDb,
    date_from: Annotated[datetime, Query()],
    date_to: Annotated[datetime, Query()],
) -> list[HealthTrendPoint]:
    return [HealthTrendPoint(**p) for p in repo.trend(db, date_from, date_to)]


@router.get("/breakdown", response_model=list[HealthBreakdownRow], summary="按维度下钻")
def breakdown(
    db: AdminDb,
    date_from: Annotated[datetime, Query()],
    date_to: Annotated[datetime, Query()],
    dim: Annotated[str, Query(pattern="^(event|app_ver|oem)$")] = "event",
) -> list[HealthBreakdownRow]:
    return [HealthBreakdownRow(**r) for r in repo.breakdown(db, date_from, date_to, dim)]
