"""Operational metrics endpoints — MTTD, MTTR, Detection Efficacy, and more.

Provides operational KPIs for security teams with trend analysis
and team-level breakdowns.
"""

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

# 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.services.operational_metrics_service
from app.services.operational_metrics_service import (
    get_metrics_by_team,
    get_operational_trend,
)

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


# ── GET /metrics/operational ──────────────────────────────────────────


@router.get("")
# Define function operational_metrics
def operational_metrics(
    # Entry: db
    db: Session = Depends(get_db),
    # Entry: current_user
    current_user: User = Depends(get_current_user),
) -> dict:
    """Get all operational metrics (MTTD, MTTR, etc.) — cached for 5 min."""
    # Import get_operational_metrics_cached from app.services.score_cache
    from app.services.score_cache import get_operational_metrics_cached

    # Return get_operational_metrics_cached(db)
    return get_operational_metrics_cached(db)


# ── GET /metrics/operational/trend ────────────────────────────────────


@router.get("/trend")
# Define function operational_trend
def operational_trend(
    # Entry: period
    period: str = Query("90d", pattern="^(30d|90d|1y)$"),
    # Entry: db
    db: Session = Depends(get_db),
    # Entry: current_user
    current_user: User = Depends(get_current_user),
) -> dict:
    """Get weekly trend data for operational metrics."""
    # Return get_operational_trend(db, period)
    return get_operational_trend(db, period)


# ── GET /metrics/operational/by-team ──────────────────────────────────


@router.get("/by-team")
# Define function metrics_by_team
def metrics_by_team(
    # Entry: db
    db: Session = Depends(get_db),
    # Entry: current_user
    current_user: User = Depends(get_current_user),
) -> dict:
    """Get metrics broken down by Red Team vs Blue Team."""
    # Return get_metrics_by_team(db)
    return get_metrics_by_team(db)
