from __future__ import annotations

from datetime import datetime

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

from app.core.database import get_db
from app.core.dependencies import get_current_admin
from app.core.responses import api_success
from app.models.admin import Admin
from app.schemas.admin import DashboardStats
from app.services.admin_service import AdminDashboardService

router = APIRouter()


@router.get("/dashboard")
def dashboard(
    start: str = Query(default="", description="开始日期 YYYY-MM-DD"),
    end: str = Query(default="", description="结束日期 YYYY-MM-DD"),
    db: Session = Depends(get_db),
    current_admin: Admin = Depends(get_current_admin),
) -> dict:
    start_dt = datetime.strptime(start, "%Y-%m-%d") if start else None
    if end:
        end_dt = datetime.strptime(end, "%Y-%m-%d")
        end_dt = end_dt.replace(hour=23, minute=59, second=59)
    else:
        end_dt = None
    stats = AdminDashboardService.stats(db, start_dt, end_dt)
    return api_success(DashboardStats.model_validate(stats).model_dump())
