"""
PRD-HWONB-012 §2 — Documents & Agreement (incl. UMA) request/response schemas.

Pydantic v2 style, matching schemas/application.py's house style (plain
BaseModel, ConfigDict(from_attributes=True) where a schema is built directly
from an ORM row).
"""

from __future__ import annotations

from datetime import datetime
from typing import Dict, List, Optional

from pydantic import BaseModel, ConfigDict, Field

from src.apps.merchant_onboarding.helpers.field_mapping import DOC_TYPE_CHOICES

# Re-exported for the router/services layer so nobody hardcodes the enum a
# second time — see helpers/field_mapping.py::DOC_TYPE_CHOICES (C-20 — `Tax`
# is Title-case, not `TAX`; 15 codes total, CERT-confirmed verbatim).
VALID_DOC_TYPES = DOC_TYPE_CHOICES


class DocumentResponse(BaseModel):
    """One row from `merchant_onboarding_documents` — used by both the
    upload response and each item in the list response."""

    model_config = ConfigDict(from_attributes=True)

    id: int
    doc_type: str
    file_name: str
    content_type: str
    size_bytes: int

    # GP's attachment id (PRD-HWONB-012 §2.5, C-22 — "attachmentId", not
    # "docId") — stored/exposed as a string for consistency with the other
    # tsys_*_ref columns across this module.
    tsys_upload_ref: Optional[str] = None
    tsys_synced_at: Optional[datetime] = None
    tsys_errors: Optional[List[Dict[str, str]]] = None

    created_at: datetime


class DocumentListResponse(BaseModel):
    """GET /onboarding/documents — local rows, reconciled best-effort against
    GP's own list (PRD-HWONB-012 §4 — "for resume/audit")."""

    documents: List[DocumentResponse] = Field(default_factory=list)
    # True only when SMA (the one unconditionally-required doc type) has been
    # uploaded — mirrors section_state["documents"]["pushed"] so the wizard
    # doesn't need a second round-trip to know if it can reach Review/Submit.
    sma_uploaded: bool = False


class DocumentDeleteResponse(BaseModel):
    """DELETE /onboarding/documents/{id} — PRD-HWONB-012 §2.5 (C-22: GP's own
    delete response includes a DELETE_ATTACHMENTS action object; this is
    HubWallet's own normalized shape, not a passthrough of GP's raw body)."""

    deleted: bool = True
    id: int
    doc_type: str


class UmaResponseMeta(BaseModel):
    """Not used as a response body (the UMA endpoint streams a binary PDF),
    kept here only as a documented reference for the content the proxy
    returns — see router.py::get_uma."""

    content_type: str = "application/pdf"
