from fastapi import APIRouter

from app.services.d2l import D2LService
from app.services.repository import get_repository


router = APIRouter(prefix="/api/dashboard", tags=["dashboard"])
repo = get_repository()
d2l = D2LService()


@router.get("/summary")
async def dashboard_summary():
    courses = d2l.get_courses()
    assignments = repo.list_assignments()
    submissions = repo.list_submissions()
    recordings = repo.list_recordings()
    sync_runs = repo.list_sync_runs()
    return {
        "backend": repo.backend,
        "counts": {
            "courses": len(courses),
            "assignments": len(assignments),
            "submissions": len(submissions),
            "recordings": len(recordings),
        },
        "latest_sync": sync_runs[-1] if sync_runs else None,
        "recent_assignments": assignments[:8],
        "recent_submissions": submissions[:8],
        "recent_recordings": recordings[:8],
    }
