"""/api/hub/* — the Hub v0 cross-tenant read surface (super-admin ONLY).

CRITICAL: these five reads are DELIBERATELY cross-tenant — they do NOT
apply ``authz.scope()`` / the tenant filter, so each returns EVERY
tenant's rows. That makes the whole surface super-admin-only:
``services.hub.require_super_admin`` gates every function (403 FORBIDDEN
for a non-super-admin, and for a super admin under view-as), refused
BEFORE any query runs. The hub paths are ALSO tenant-exempt
(``deps._TENANT_EXEMPT_PREFIXES``) because hub data is not tenant-scoped,
so a cross-tenant read must not trip the NO_TENANT gate.

Thin HTTP layer: the gate + the SQL live in ``services/hub``; this only
maps ``ServiceError`` -> the structured ApiError triple.
"""

from __future__ import annotations

import uuid

from fastapi import APIRouter

from app.routers.deps import ActorDep, ApiError, SessionDep
from app.services import hub as hub_svc
from app.services.errors import ServiceError

router = APIRouter()


def _run(fn, *args):
    try:
        return fn(*args)
    except ServiceError as exc:
        raise ApiError(exc.status, exc.code, exc.summary, exc.detail) from exc


@router.get("/api/hub/projects")
def hub_projects(session: SessionDep, actor: ActorDep) -> dict:
    return _run(hub_svc.projects, session, actor)


@router.get("/api/hub/organizations")
def hub_organizations(session: SessionDep, actor: ActorDep) -> dict:
    return _run(hub_svc.organizations, session, actor)


@router.get("/api/hub/organizations/{party_id}/detail")
def hub_organization_detail(
    party_id: uuid.UUID, session: SessionDep, actor: ActorDep
) -> dict:
    return _run(hub_svc.organization_detail, session, actor, party_id)


@router.get("/api/hub/people")
def hub_people(session: SessionDep, actor: ActorDep) -> dict:
    return _run(hub_svc.people, session, actor)


@router.get("/api/hub/tenants")
def hub_tenants(session: SessionDep, actor: ActorDep) -> dict:
    return _run(hub_svc.tenants, session, actor)


@router.get("/api/hub/accounts")
def hub_accounts(session: SessionDep, actor: ActorDep) -> dict:
    return _run(hub_svc.accounts, session, actor)
