"""Advanced metrics endpoints — coverage by tactic, never-tested, avg validation time."""

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

from app.database import get_db
from app.dependencies.auth import get_current_user
from app.models.user import User
from app.services import advanced_metrics_service

router = APIRouter(prefix="/metrics/advanced", tags=["advanced-metrics"])


@router.get("/coverage-by-tactic")
def coverage_by_tactic(
    db: Session = Depends(get_db),
    user: User = Depends(get_current_user),
):
    """Coverage percentage broken down by MITRE ATT&CK tactic."""
    return advanced_metrics_service.get_coverage_by_tactic(db)


@router.get("/never-tested")
def never_tested_techniques(
    db: Session = Depends(get_db),
    user: User = Depends(get_current_user),
):
    """Techniques that have never had a test created."""
    return advanced_metrics_service.get_never_tested_techniques(db)


@router.get("/avg-validation-time")
def avg_validation_time(
    db: Session = Depends(get_db),
    user: User = Depends(get_current_user),
):
    """Average time from test creation to validation, computed from audit logs.

    Returns overall average and per-phase averages where data is available.
    """
    return advanced_metrics_service.get_avg_validation_time(db)


@router.get("/detection-rate-trend")
def detection_rate_trend(
    db: Session = Depends(get_db),
    user: User = Depends(get_current_user),
):
    """Monthly detection rate trend for the last 12 months."""
    return advanced_metrics_service.get_detection_rate_trend(db)
