"""
AwoooP Operator Console — Contracts List API
=============================================
ADR-106（AwoooP Agent Platform），ADR-107/ADR-112（Contract Revision）
2026-05-05 ogt + Claude Sonnet 4.6
"""

from __future__ import annotations

from datetime import datetime
from typing import Any
from uuid import UUID

from fastapi import APIRouter, Query
from pydantic import BaseModel

from src.services.platform_operator_service import list_contracts as list_contracts_svc

router = APIRouter()


class ContractItem(BaseModel):
    revision_id: UUID
    contract_id: str
    contract_family: str
    lifecycle_status: str
    body_hash: str
    version_major: int
    version_minor: int
    created_at: datetime
    project_id: str


class ListContractsResponse(BaseModel):
    contracts: list[ContractItem]
    total: int


@router.get(
    "/contracts",
    response_model=ListContractsResponse,
    summary="列出合約 Revisions",
    description=(
        "返回 awooop_contract_revisions，支援 project_id / lifecycle_status filter。\n\n"
        "- 按 created_at DESC 排序，最多 200 筆\n"
        "- ADR-107/ADR-112：append-only revision 表，只查不寫"
    ),
)
async def list_contracts(
    project_id: str | None = Query(None, description="租戶 ID（可選）"),
    lifecycle_status: str | None = Query(None, description="lifecycle status filter（draft/published/active/revoked）"),
) -> dict[str, Any]:
    return await list_contracts_svc(project_id=project_id, lifecycle_status=lifecycle_status)
