"""Analytics endpoints — flat JSON optimized for PowerBI / BI tools.

Returns complete datasets without pagination so BI tools can ingest
directly from URL. All endpoints require authentication.
"""

# 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, require_role from app.dependencies.auth
from app.dependencies.auth import get_current_user, require_role

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

# Import analytics_service from app.services
from app.services import analytics_service

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


# Apply the @router.get decorator
@router.get("/coverage")
# Define function analytics_coverage
def analytics_coverage(
    # Entry: db
    db: Session = Depends(get_db),
    # Entry: user
    user: User = Depends(get_current_user),
) -> list:
    """Coverage per technique — flat format for BI dashboards."""
    # Return analytics_service.get_coverage_analytics(db)
    return analytics_service.get_coverage_analytics(db)


# Apply the @router.get decorator
@router.get("/tests")
# Define function analytics_tests
def analytics_tests(
    # Entry: date_from
    date_from: str = Query(None, description="ISO date filter (>=)"),
    # Entry: date_to
    date_to: str = Query(None, description="ISO date filter (<=)"),
    # Entry: db
    db: Session = Depends(get_db),
    # Entry: user
    user: User = Depends(get_current_user),
) -> list:
    """All tests with timestamps — flat format for BI dashboards."""
    # Return analytics_service.get_tests_analytics(
    return analytics_service.get_tests_analytics(
        db, date_from=date_from, date_to=date_to
    )


# Apply the @router.get decorator
@router.get("/trends")
# Define function analytics_trends
def analytics_trends(
    # Entry: db
    db: Session = Depends(get_db),
    # Entry: user
    user: User = Depends(get_current_user),
) -> list:
    """Historical coverage snapshots for trend visualization."""
    # Return analytics_service.get_trends_analytics(db)
    return analytics_service.get_trends_analytics(db)


# Apply the @router.get decorator
@router.get("/operators")
# Define function analytics_operators
def analytics_operators(
    # Entry: db
    db: Session = Depends(get_db),
    # Entry: user
    user: User = Depends(require_role("admin")),
) -> list:
    """Per-operator metrics — for workload management dashboards."""
    # Return analytics_service.get_operators_analytics(db)
    return analytics_service.get_operators_analytics(db)
