"""Coverage-metrics endpoints.

Provides aggregated views of MITRE ATT&CK technique coverage for
dashboards and reporting.  V2 adds pipeline, team-activity, and
validation-rate endpoints for the Red/Blue workflow.

Thin HTTP adapter: delegates all data logic to metrics_query_service.
"""

# Import APIRouter, Depends from fastapi
from fastapi import APIRouter, Depends

# Import Session from sqlalchemy.orm
from sqlalchemy.orm import Session

# Import get_db from app.database
from app.database import get_db

# Import get_current_user from app.dependencies.auth
from app.dependencies.auth import get_current_user

# Import User from app.models.user
from app.models.user import User

# Import  from app.schemas.metrics
from app.schemas.metrics import (
    CoverageSummary,
    RecentTestItem,
    TacticCoverage,
    TeamActivity,
    TestPipelineCounts,
    ValidationRate,
)

# Import  from app.services.metrics_query_service
from app.services.metrics_query_service import (
    get_coverage_by_tactic,
    get_coverage_summary,
    get_recent_tests,
    get_team_activity,
    get_test_pipeline_counts,
    get_validation_rate,
)

# Assign router = APIRouter(prefix="/metrics", tags=["metrics"])
router = APIRouter(prefix="/metrics", tags=["metrics"])


# ---------------------------------------------------------------------------
# GET /metrics/summary
# ---------------------------------------------------------------------------


@router.get("/summary", response_model=CoverageSummary)
# Define function coverage_summary
def coverage_summary(
    # Entry: db
    db: Session = Depends(get_db),
    # Entry: current_user
    current_user: User = Depends(get_current_user),
) -> CoverageSummary:
    """Return a global coverage summary across all techniques."""
    # Return get_coverage_summary(db)
    return get_coverage_summary(db)


# ---------------------------------------------------------------------------
# GET /metrics/by-tactic
# ---------------------------------------------------------------------------


@router.get("/by-tactic", response_model=list[TacticCoverage])
# Define function coverage_by_tactic
def coverage_by_tactic(
    # Entry: db
    db: Session = Depends(get_db),
    # Entry: current_user
    current_user: User = Depends(get_current_user),
) -> list[TacticCoverage]:
    """Return coverage breakdown grouped by tactic."""
    # Return get_coverage_by_tactic(db)
    return get_coverage_by_tactic(db)


# ---------------------------------------------------------------------------
# GET /metrics/test-pipeline  —  counters per pipeline state
# ---------------------------------------------------------------------------


@router.get("/test-pipeline", response_model=TestPipelineCounts)
# Define function test_pipeline
def test_pipeline(
    # Entry: db
    db: Session = Depends(get_db),
    # Entry: current_user
    current_user: User = Depends(get_current_user),
) -> TestPipelineCounts:
    """Return how many tests are in each pipeline state."""
    # Return get_test_pipeline_counts(db)
    return get_test_pipeline_counts(db)


# ---------------------------------------------------------------------------
# GET /metrics/team-activity  —  activity per team
# ---------------------------------------------------------------------------


@router.get("/team-activity", response_model=list[TeamActivity])
# Define function team_activity
def team_activity(
    # Entry: db
    db: Session = Depends(get_db),
    # Entry: current_user
    current_user: User = Depends(get_current_user),
) -> list[TeamActivity]:
    """Return activity summary for Red and Blue teams."""
    # Return get_team_activity(db)
    return get_team_activity(db)


# ---------------------------------------------------------------------------
# GET /metrics/validation-rate  —  approval / rejection rates
# ---------------------------------------------------------------------------


@router.get("/validation-rate", response_model=list[ValidationRate])
# Define function validation_rate
def validation_rate(
    # Entry: db
    db: Session = Depends(get_db),
    # Entry: current_user
    current_user: User = Depends(get_current_user),
) -> list[ValidationRate]:
    """Return approval and rejection rates for Red Lead and Blue Lead."""
    # Return get_validation_rate(db)
    return get_validation_rate(db)


# ---------------------------------------------------------------------------
# GET /metrics/recent-tests  —  latest 10 updated tests
# ---------------------------------------------------------------------------


@router.get("/recent-tests", response_model=list[RecentTestItem])
# Define function recent_tests
def recent_tests(
    # Entry: db
    db: Session = Depends(get_db),
    # Entry: current_user
    current_user: User = Depends(get_current_user),
) -> list[RecentTestItem]:
    """Return the 10 most recently created tests."""
    # Return get_recent_tests(db, limit=10)
    return get_recent_tests(db, limit=10)
