"""Pydantic schemas returned by the formal workbench API."""
from __future__ import annotations

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

from pydantic import BaseModel, ConfigDict, Field


class ApiSchema(BaseModel):
    model_config = ConfigDict(from_attributes=True)


class Page(ApiSchema):
    total: int
    page: int = 1
    size: int = 50
    items: list[dict[str, Any]] = Field(default_factory=list)


class ErrorResponse(ApiSchema):
    code: str
    message: str
    detail: dict[str, Any] = Field(default_factory=dict)


class QueryBatchSchema(ApiSchema):
    id: UUID
    name: str
    source_type: str
    generation_method: str | None = None
    target_platforms: list[str] = Field(default_factory=list)
    status: str
    created_at: datetime | None = None
    updated_at: datetime | None = None


class QuerySchema(ApiSchema):
    id: UUID
    batch_id: UUID | None = None
    query_text: str
    axes: dict[str, Any] = Field(default_factory=dict)
    keep: bool | None = None
    filter_reason: str | None = None
    status: str
    sort_order: int = 0


class MediaAssetSchema(ApiSchema):
    id: UUID
    item_id: UUID
    media_type: str
    source_url: str | None = None
    oss_url: str | None = None
    cdn_url: str | None = None
    position: int = 0
    status: str


class ItemClassificationSchema(ApiSchema):
    id: UUID
    item_id: UUID
    is_creation_knowledge: bool | None = None
    label: str | None = None
    confidence: float | None = None
    reason: str | None = None
    status: str
    error_message: str | None = None


class CandidateItemSchema(ApiSchema):
    id: UUID
    platform: str
    platform_item_id: str | None = None
    unique_key: str | None = None
    canonical_url: str | None = None
    content_type: str | None = None
    content_mode: str | None = None
    title: str | None = None
    author_name: str | None = None
    body_text: str | None = None
    raw_summary: str | None = None
    status: str
    metadata: dict[str, Any] = Field(default_factory=dict)
    error_message: str | None = None
    decode_summary: dict[str, Any] | None = None
    media_assets: list[MediaAssetSchema] = Field(default_factory=list)
    classification: ItemClassificationSchema | None = None


class AcquisitionRunSummarySchema(ApiSchema):
    id: UUID
    run_key: str | None = None
    batch_id: UUID | None = None
    status: str
    query_count: int = 0
    job_count: int = 0
    candidate_count: int = 0
    creation_hit_count: int = 0
    decoded_count: int = 0
    payload_count: int = 0
    queries: list[dict[str, Any]] = Field(default_factory=list)
    started_at: datetime | None = None
    finished_at: datetime | None = None


class DecodeResultSchema(ApiSchema):
    id: UUID
    item_id: UUID
    status: str
    read_result: dict[str, Any] = Field(default_factory=dict)
    gate_result: dict[str, Any] = Field(default_factory=dict)
    framing_result: dict[str, Any] = Field(default_factory=dict)


class KnowledgeParticleSchema(ApiSchema):
    id: UUID
    decode_result_id: UUID | None = None
    item_id: UUID | None = None
    parent_particle_id: UUID | None = None
    particle_type: str
    title: str
    business_stage: str | None = None
    creation_stage: str | None = None
    content: dict[str, Any] = Field(default_factory=dict)
    status: str


class ScopeResultSchema(ApiSchema):
    id: UUID
    particle_id: UUID | None = None
    item_id: UUID | None = None
    scope_type: str
    scope_value: str
    is_reused: bool | None = None
    matched_scope_id: str | None = None
    confidence: float | None = None
    status: str


class PayloadDraftSchema(ApiSchema):
    id: UUID
    particle_id: UUID | None = None
    item_id: UUID | None = None
    payload: dict[str, Any] = Field(default_factory=dict)
    review_status: str
    ingest_ready: bool
    status: str


class IngestRecordSchema(ApiSchema):
    id: UUID
    payload_draft_id: UUID | None = None
    target_system: str
    target_id: str | None = None
    status: str
    attempt_count: int = 0
    error_message: str | None = None
