"""Super admin dashboard summary.""" from fastapi import APIRouter, Depends from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select, func, and_ from datetime import date, timedelta from app.auth.dependencies import require_super_admin from app.database import get_db from app.models.user import HubUser from app.models.school import School, SchoolStatus from app.models.license import License, LicenseStatus from app.models.sms import SmsJob, SmsJobStatus from app.models.billing import Invoice, InvoiceStatus from app.models.ticket import SupportTicket, TicketStatus router = APIRouter(prefix="/api/dashboard", tags=["dashboard"]) @router.get("/summary") async def get_summary( _admin: HubUser = Depends(require_super_admin), db: AsyncSession = Depends(get_db), ): total_schools = (await db.execute(select(func.count()).select_from(School))).scalar_one() active_schools = (await db.execute(select(func.count()).where(School.status == SchoolStatus.active))).scalar_one() suspended_schools = (await db.execute(select(func.count()).where(School.status == SchoolStatus.suspended))).scalar_one() expiring_soon = (await db.execute( select(func.count()).where( and_(License.expires_at != None, License.expires_at <= date.today() + timedelta(days=30), License.status == LicenseStatus.active) ) )).scalar_one() open_tickets = (await db.execute( select(func.count()).where(SupportTicket.status.in_([TicketStatus.open, TicketStatus.in_progress])) )).scalar_one() pending_invoices = (await db.execute( select(func.count()).where(Invoice.status.in_([InvoiceStatus.sent, InvoiceStatus.overdue])) )).scalar_one() sms_today = (await db.execute( select(func.count()).where( and_(func.date(SmsJob.created_at) == date.today(), SmsJob.status == SmsJobStatus.sent) ) )).scalar_one() sms_pending = (await db.execute( select(func.count()).where(SmsJob.status == SmsJobStatus.pending) )).scalar_one() return { "schools": {"total": total_schools, "active": active_schools, "suspended": suspended_schools}, "licenses": {"expiring_soon": expiring_soon}, "tickets": {"open": open_tickets}, "invoices": {"pending": pending_invoices}, "sms": {"sent_today": sms_today, "pending": sms_pending}, }