"""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.
"""

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

from app.database import get_db
from app.dependencies.auth import get_current_user, require_role
from app.models.user import User
from app.services import analytics_service

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


@router.get("/coverage")
def analytics_coverage(
    db: Session = Depends(get_db),
    user: User = Depends(get_current_user),
):
    """Coverage per technique — flat format for BI dashboards."""
    return analytics_service.get_coverage_analytics(db)


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


@router.get("/trends")
def analytics_trends(
    db: Session = Depends(get_db),
    user: User = Depends(get_current_user),
):
    """Historical coverage snapshots for trend visualization."""
    return analytics_service.get_trends_analytics(db)


@router.get("/operators")
def analytics_operators(
    db: Session = Depends(get_db),
    user: User = Depends(require_role("admin")),
):
    """Per-operator metrics — for workload management dashboards."""
    return analytics_service.get_operators_analytics(db)
