"""/api/summary — the month dashboard (summary.md parity): one row per
project comparing prev/this/all-time, plus SQL-side totals. Money is
shaped away entirely for viewers without can_see_income (T-PRM-05
keeps cost visible — cost was never owner-only — but billout and
adjusted lines never leave the server)."""

from __future__ import annotations

from fastapi import APIRouter, Query

from app.routers.deps import ActorDep, SessionDep, tenant_currency
from app.services.reporting.summary import parse_month, summary_by_project

router = APIRouter()


@router.get("/api/summary")
def summary(
    session: SessionDep,
    actor: ActorDep,
    month: str | None = Query(None),
    operator: str | None = None,
    client: str | None = None,
    user: str | None = None,
    source_email: str | None = None,
) -> dict:
    this = parse_month(month)
    # B8 parity: view-as of a non-owner scopes the summary to imports
    # the effective user uploaded (scope() also enforces this).
    imported_by = None
    if actor.is_viewing_as and actor.role != "owner" and not actor.is_super_admin:
        imported_by = actor.user_id
    return summary_by_project(
        session,
        actor,
        this,
        operator=(operator or "").strip() or None,
        client=(client or "").strip() or None,
        user=(user or "").strip() or None,
        source_email=(source_email or "").strip() or None,
        imported_by=imported_by,
        zero_currency=tenant_currency(actor),
    )
