"""Formal payload draft API routes."""
from __future__ import annotations

from typing import Any
from uuid import UUID

from fastapi import APIRouter, Depends, HTTPException, Query

from app.dependencies import get_decode_repository
from app.schemas import PayloadDraftSchema

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


@router.get("/drafts")
def payload_drafts(
    item_id: UUID | None = Query(default=None),
    repo: Any = Depends(get_decode_repository),
) -> dict[str, Any]:
    list_drafts = getattr(repo, "list_payload_drafts", None)
    if list_drafts is None:
        raise HTTPException(status_code=501, detail="payload draft reader is not implemented")
    rows = list_drafts(item_id=item_id)
    return {
        "items": [
            PayloadDraftSchema.model_validate(row).model_dump(mode="json")
            for row in rows
        ]
    }
