"""Coverage routes."""

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

from app.core.db import get_db
from app.schemas.coverage import CoverageSummaryRead
from app.services import coverage_engine

router = APIRouter(prefix="/coverage", tags=["coverage"])


@router.get("", response_model=CoverageSummaryRead)
def get_coverage(
    engagement_id: int | None = Query(default=None, ge=1),
    db: Session = Depends(get_db),
) -> CoverageSummaryRead:
    """Return coverage score and gap analysis for one engagement."""
    if engagement_id is None:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="engagement_id is required",
        )

    try:
        return coverage_engine.get_coverage(db=db, engagement_id=engagement_id)
    except LookupError as exc:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(exc)) from exc
