"""Formal decode-content domain models."""
from __future__ import annotations

from datetime import datetime
from typing import Any, Literal
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field


JsonDict = dict[str, Any]
ParticleType = Literal["what", "how", "why"]


class DecodeModel(BaseModel):
    model_config = ConfigDict(extra="forbid")


class ReadCard(DecodeModel):
    index: int
    kind: str = "image"
    url: str | None = None
    timestamp: float | None = None
    start: float | None = None
    end: float | None = None
    content: str = ""


class ReadResult(DecodeModel):
    text: str = ""
    cards: list[ReadCard] = Field(default_factory=list)
    media: JsonDict = Field(default_factory=dict)
    is_empty: bool = False


class GateResult(DecodeModel):
    passed: bool
    reason: str = ""
    details: JsonDict = Field(default_factory=dict)


class HowStep(DecodeModel):
    input: str = ""
    directive: str = ""
    output: str = ""
    scopes: list[JsonDict] = Field(default_factory=list)


class KnowledgeParticle(DecodeModel):
    id: UUID | None = None
    decode_result_id: UUID | None = None
    item_id: UUID | None = None
    parent_particle_id: UUID | None = None
    particle_type: ParticleType
    title: str
    business_stage: str | None = None
    creation_stage: str | None = None
    content: JsonDict = Field(default_factory=dict)
    sort_order: int = 0
    status: str = "draft"
    metadata: JsonDict = Field(default_factory=dict)
    error_message: str | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None


class ScopeResult(DecodeModel):
    id: UUID | None = None
    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
    evidence: JsonDict = Field(default_factory=dict)
    status: str = "draft"
    error_message: str | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None


class PayloadDraft(DecodeModel):
    id: UUID | None = None
    particle_id: UUID | None = None
    item_id: UUID | None = None
    payload: JsonDict = Field(default_factory=dict)
    review_status: str = "pending"
    ingest_ready: bool = False
    status: str = "draft"
    error_message: str | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None


class IngestRecord(DecodeModel):
    id: UUID | None = None
    payload_draft_id: UUID | None = None
    target_system: str
    target_id: str | None = None
    status: str = "pending"
    attempt_count: int = 0
    response_payload: JsonDict = Field(default_factory=dict)
    error_message: str | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None


class DecodeJob(DecodeModel):
    id: UUID | None = None
    item_id: UUID
    status: str = "pending"
    attempt_count: int = 0
    metadata: JsonDict = Field(default_factory=dict)
    error_message: str | None = None
    started_at: datetime | None = None
    finished_at: datetime | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None


class DecodeResult(DecodeModel):
    id: UUID | None = None
    decode_job_id: UUID | None = None
    item_id: UUID
    read_result: JsonDict = Field(default_factory=dict)
    gate_result: JsonDict = Field(default_factory=dict)
    framing_result: JsonDict = Field(default_factory=dict)
    status: str = "draft"
    error_message: str | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None


class ContractSnapshot(DecodeModel):
    id: UUID | None = None
    contract_name: str
    contract_type: str
    version_label: str | None = None
    content_hash: str | None = None
    source_path: str | None = None
    snapshot: JsonDict = Field(default_factory=dict)
    created_at: datetime | None = None


Knowledge = KnowledgeParticle
