"""/api/analytics/* — read-only business-performance / decision-support views.

Distinct from the operational planning modules: these are analytical rollups for
tracking growth, margin and mix over time (category / channel / country).
"""
from __future__ import annotations

from typing import Optional

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

from backend.api.middleware.auth import CurrentUser, require_section
from backend.models.database import get_db
from backend.services import analytics_service as svc

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

_REQ = Depends(require_section("executive_dashboard"))


@router.get("/category-performance")
def category_performance(
    granularity: str = Query("month", pattern="^(month|week)$"),
    channel: Optional[str] = Query(None, pattern="^(retail|webshop|wholesale)$"),
    country: Optional[str] = Query(None),
    category: Optional[str] = Query(None),
    db: Session = Depends(get_db),
    _: CurrentUser = _REQ,
) -> dict:
    return svc.category_performance(
        db, granularity=granularity, channel=channel, country=country,
        category=category,
    )


@router.get("/buyer-performance")
def buyer_performance(
    country: Optional[str] = Query(None),
    category: Optional[str] = Query(None),
    db: Session = Depends(get_db),
    _: CurrentUser = _REQ,
) -> dict:
    return svc.buyer_performance(db, country=country, category=category)


@router.get("/store-performance")
def store_performance(
    country: Optional[str] = Query(None),
    category: Optional[str] = Query(None),
    db: Session = Depends(get_db),
    _: CurrentUser = _REQ,
) -> dict:
    return svc.store_performance(db, country=country, category=category)


@router.get("/profitability")
def profitability(
    channel: Optional[str] = Query(None, pattern="^(retail|webshop|wholesale)$"),
    country: Optional[str] = Query(None),
    db: Session = Depends(get_db),
    _: CurrentUser = _REQ,
) -> dict:
    return svc.profitability(db, channel=channel, country=country)


@router.get("/forecast-outlook")
def forecast_outlook(db: Session = Depends(get_db), _: CurrentUser = _REQ) -> dict:
    return svc.forecast_outlook(db)


@router.get("/wholesale-fa")
def wholesale_fa(
    from_yw: Optional[int] = Query(None),
    to_yw: Optional[int] = Query(None),
    db: Session = Depends(get_db),
    _: CurrentUser = _REQ,
) -> dict:
    return svc.wholesale_fa(db, from_yw=from_yw, to_yw=to_yw)


@router.get("/wholesale-fa-review")
def wholesale_fa_review(
    from_yw: Optional[int] = Query(None),
    to_yw: Optional[int] = Query(None),
    db: Session = Depends(get_db),
    _: CurrentUser = _REQ,
) -> dict:
    return svc.wholesale_fa_review(db, from_yw=from_yw, to_yw=to_yw)


@router.get("/wholesale-fa-review/export")
def wholesale_fa_review_export(
    from_yw: Optional[int] = Query(None),
    to_yw: Optional[int] = Query(None),
    db: Session = Depends(get_db),
    _: CurrentUser = _REQ,
) -> Response:
    data = svc.wholesale_fa_excel(db, from_yw=from_yw, to_yw=to_yw)
    return Response(
        content=data,
        media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        headers={"Content-Disposition": "attachment; filename=wholesale_fa_review.xlsx"},
    )
