"""/api/executive/* — read-only Executive Dashboard for CEO / CFO / Uprava.

A single rollup endpoint (`/dashboard`) returns every section in one call so
the UI mounts with one request. Each section is wrapped in try/except inside
the service layer — a broken query never breaks the whole page.

Access: `executive_dashboard` permission key (Admin + Uprava).
"""
from __future__ import annotations

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

from backend.api.middleware.auth import CurrentUser, require_section
from backend.models.database import get_db
from backend.schemas.executive import (
    ExecutiveDashboard, ForecastAccuracySummary, InventorySnapshot,
    InventoryTrend, RevenuePulse, StockoutRisk, SupplyArrivals,
)
from backend.services import executive_service as svc

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

_REQ = Depends(require_section("executive_dashboard"))


@router.get("/dashboard", response_model=ExecutiveDashboard)
def get_dashboard(db: Session = Depends(get_db),
                  _: CurrentUser = _REQ) -> ExecutiveDashboard:
    return svc.get_executive_dashboard(db)


@router.get("/inventory", response_model=InventorySnapshot)
def get_inventory(db: Session = Depends(get_db),
                  _: CurrentUser = _REQ) -> InventorySnapshot:
    return svc.get_inventory_snapshot(db)


@router.get("/revenue", response_model=RevenuePulse)
def get_revenue(db: Session = Depends(get_db),
                _: CurrentUser = _REQ) -> RevenuePulse:
    return svc.get_revenue_pulse(db)


@router.get("/stockout-risks", response_model=list[StockoutRisk])
def get_stockout_risks(db: Session = Depends(get_db),
                       _: CurrentUser = _REQ) -> list[StockoutRisk]:
    return svc.get_stockout_risks(db)


@router.get("/supply-arrivals", response_model=SupplyArrivals)
def get_supply_arrivals(db: Session = Depends(get_db),
                        _: CurrentUser = _REQ) -> SupplyArrivals:
    return svc.get_supply_arrivals(db)


@router.get("/forecast-accuracy", response_model=ForecastAccuracySummary)
def get_forecast_accuracy(db: Session = Depends(get_db),
                          _: CurrentUser = _REQ) -> ForecastAccuracySummary:
    return svc.get_forecast_accuracy_summary(db)


@router.get("/inventory-trend", response_model=InventoryTrend)
def get_inventory_trend(db: Session = Depends(get_db),
                        _: CurrentUser = _REQ) -> InventoryTrend:
    return svc.get_inventory_trend(db)
