""" FastAPI Dashboard Router (/api/v1/dashboard) Provides operational metrics, today/tomorrow/week schedule, and Google Sheets sync inspection. 100% Native FastAPI - Zero Flask dependencies. """ from typing import Dict, Any from fastapi import APIRouter, Depends, HTTPException, Request, status from app.models.user import User from app.services.dashboard_service import DashboardService from dependencies import get_current_user dashboard_router = APIRouter(prefix="/api/v1/dashboard", tags=["Dashboard"]) dashboard_service = DashboardService() @dashboard_router.get("/stats") def get_stats(current_user: User = Depends(get_current_user)): """Retorna estadísticas clave y datos operativos para el Dashboard.""" return dashboard_service.get_stats(current_user=current_user) @dashboard_router.post("/sync-sheets") def sync_google_sheets( payload: Dict[str, Any], request: Request, current_user: User = Depends(get_current_user) ): """Ejecuta la sincronización con Google Sheets con filtros y selección granular.""" date_from = payload.get('date_from') date_to = payload.get('date_to') if date_from and date_to and str(date_from).strip() > str(date_to).strip(): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="La fecha de inicio (date_from) no puede ser posterior a la fecha de fin (date_to)." ) try: ip = request.client.host if request.client else '127.0.0.1' return dashboard_service.sync_google_sheets(payload, current_user=current_user, ip_address=ip) except Exception as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e) ) @dashboard_router.get("/sheets-inspect") @dashboard_router.post("/sheets-inspect") def inspect_google_sheets( request: Request, current_user: User = Depends(get_current_user) ): """Inspecciona las hojas de cálculo descubriendo carreras, materias y previsualización.""" try: # Puede venir como query params o json body data: Dict[str, Any] = dict(request.query_params) return dashboard_service.inspect_google_sheets(data) except Exception as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e) )