"""Analytics APIs — Phase 12.8."""
from __future__ import annotations

from uuid import UUID

from fastapi import APIRouter, Depends, status
from sqlalchemy.ext.asyncio import AsyncSession

from app.api.deps import get_db, get_pagination, require_tenant
from app.core.security import get_current_user
from app.schemas.analytics import (
    AnalyticsReportDefinitionCreate,
    AnalyticsReportDefinitionRead,
    AnalyticsSnapshotRead,
    AnalyticsSnapshotRefresh,
)
from app.services.analytics import AnalyticsReportDefinitionService, AnalyticsSnapshotService
from shared.pagination import PaginationParams
from shared.security import CurrentUser

analytics_reports_router = APIRouter()
analytics_snapshots_router = APIRouter()


@analytics_reports_router.post(
    "", response_model=AnalyticsReportDefinitionRead, status_code=status.HTTP_201_CREATED
)
async def create_analytics_report(
    body: AnalyticsReportDefinitionCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await AnalyticsReportDefinitionService(db).create(tenant_id, body, actor=user)


@analytics_reports_router.get("", response_model=list[AnalyticsReportDefinitionRead])
async def list_analytics_reports(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await AnalyticsReportDefinitionService(db).list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@analytics_reports_router.get("/{report_id}", response_model=AnalyticsReportDefinitionRead)
async def get_analytics_report(
    report_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await AnalyticsReportDefinitionService(db).get(tenant_id, report_id)


@analytics_snapshots_router.post(
    "/refresh", response_model=AnalyticsSnapshotRead, status_code=status.HTTP_201_CREATED
)
async def refresh_analytics_snapshot(
    body: AnalyticsSnapshotRefresh,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await AnalyticsSnapshotService(db).refresh(tenant_id, body, actor=user)


@analytics_snapshots_router.get("", response_model=list[AnalyticsSnapshotRead])
async def list_analytics_snapshots(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await AnalyticsSnapshotService(db).list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@analytics_snapshots_router.get("/{snapshot_id}", response_model=AnalyticsSnapshotRead)
async def get_analytics_snapshot(
    snapshot_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await AnalyticsSnapshotService(db).get(tenant_id, snapshot_id)
