"""
AwoooP Operator Console — Tenants List API
==========================================
ADR-106（AwoooP Agent Platform），ADR-115（Tenant Onboarding）
2026-05-05 ogt + Claude Sonnet 4.6
"""

from __future__ import annotations

from datetime import datetime
from decimal import Decimal
from typing import Any
from uuid import UUID

from fastapi import APIRouter
from pydantic import BaseModel

from src.services.platform_operator_service import list_tenants as list_tenants_svc

router = APIRouter()


class TenantItem(BaseModel):
    project_id: str
    display_name: str
    migration_mode: str
    budget_limit_usd: Decimal | None
    is_active: bool
    created_at: datetime


class TenantAssetSummary(BaseModel):
    tenant_table_count: int
    product_surface_count: int
    public_route_count: int
    public_gateway_snapshot_route_count: int
    source_candidate_repo_count: int
    source_in_scope_repo_count: int
    source_primary_ready_count: int
    owner_response_received_count: int
    owner_response_accepted_count: int
    runtime_gate_count: int
    action_button_count: int


class TenantProductSurface(BaseModel):
    product_id: str
    product_name: str
    project_id: str
    category: str
    surface_kind: str
    owner_lane: str
    coverage_status: str
    public_routes: list[str]
    source_keys: list[str]
    public_route_count: int
    source_repo_count: int
    missing_public_routes: list[str]
    owner_response_received_count: int
    owner_response_accepted_count: int
    runtime_gate_count: int
    action_button_count: int


class TenantPublicRouteAsset(BaseModel):
    domain: str
    product_id: str
    product_name: str
    category: str
    coverage_status: str
    control_tier: str
    upstream_count: int
    admin_route_count: int
    websocket_route_count: int
    public_route_smoke_required: bool
    route_smoke_accepted: bool
    owner_response_accepted: bool
    runtime_gate_count: int
    action_button_count: int
    source: str


class TenantSourceRepoAsset(BaseModel):
    github_repo: str
    source_key: str
    source_scope_id: str
    source_namespace_redacted: bool
    product_id: str
    product_name: str
    category: str
    scope_status: str
    readiness_state: str
    risk: str
    primary_ready: bool
    blocker_count: int
    runtime_gate_count: int
    action_button_count: int


class TenantAssetInventory(BaseModel):
    schema_version: str
    mode: str
    evidence_refs: list[str]
    summary: TenantAssetSummary
    products: list[TenantProductSurface]
    public_routes: list[TenantPublicRouteAsset]
    source_repos: list[TenantSourceRepoAsset]
    boundaries: list[str]


class ListTenantsResponse(BaseModel):
    tenants: list[TenantItem]
    total: int
    asset_inventory: TenantAssetInventory


@router.get(
    "/tenants",
    response_model=ListTenantsResponse,
    summary="列出所有租戶",
    description=(
        "返回所有 awooop_projects 記錄（含已停用）。\n\n"
        "ADR-106/ADR-115：Operator Console 使用，不依 RLS 過濾。"
    ),
)
async def list_tenants() -> dict[str, Any]:
    return await list_tenants_svc()
