from __future__ import annotations

from typing import Literal

from pydantic import BaseModel, ConfigDict, Field


class SoatStructuredModel(BaseModel):
    model_config = ConfigDict(populate_by_name=True, extra="ignore")

    schema_version: Literal["v1"] = Field(default="v1", alias="sv")


class SoatCodeCandidateStructured(BaseModel):
    model_config = ConfigDict(populate_by_name=True, extra="ignore")

    codigo: str = Field(alias="c")
    descripcion: str = Field(alias="d")
    justificacion: str | None = Field(default=None, alias="j")
    score: float | None = Field(default=None, alias="s")
    tipo: str | None = Field(default=None, alias="t")


class SoatEvidenceItemStructured(BaseModel):
    model_config = ConfigDict(populate_by_name=True, extra="ignore")

    page: int | None = Field(default=None, alias="p")
    extracto: str = Field(alias="e")
    score: float | None = Field(default=None, alias="s")


class SoatGlosaStructured(SoatStructuredModel):
    resumen: str = Field(alias="rs")
    riesgos_altos: list[str] = Field(default_factory=list, alias="ra")
    riesgos_medios: list[str] = Field(default_factory=list, alias="rm")
    mitigaciones: list[str] = Field(default_factory=list, alias="mt")
    nota_sugerida: str | None = Field(default=None, alias="nt")


class SoatValidationStructured(SoatStructuredModel):
    estado_validacion: str = Field(alias="ev")
    verificacion_manual: str = Field(alias="vm")
    coherencia_documental: str = Field(alias="cd")
    riesgo_glosa: str = Field(alias="rg")
    recomendaciones: str = Field(alias="rc")
    pertinencia_codigos: str = Field(alias="pc")
    nota_aclaratoria: str = Field(alias="na")
    resumen_validacion: str = Field(alias="rv")
    cie10_propuestos: list[SoatCodeCandidateStructured] = Field(default_factory=list, alias="dx")
    cups_propuestos: list[SoatCodeCandidateStructured] = Field(default_factory=list, alias="cp")
    evidencia_soat: list[SoatEvidenceItemStructured] = Field(default_factory=list, alias="es")
    contexto_cups: str | None = Field(default=None, alias="cc")
    contexto_cie10: str | None = Field(default=None, alias="cx")


class SoatGeneratedCodeStructured(SoatStructuredModel):
    codigo_soat: str = Field(default="", alias="cs")
    descripcion: str = Field(alias="d")
    cie10_principal: SoatCodeCandidateStructured | None = Field(default=None, alias="c1")
    cie10_secundarios: list[SoatCodeCandidateStructured] = Field(default_factory=list, alias="c2")
    cups_principal: SoatCodeCandidateStructured | None = Field(default=None, alias="p1")
    cups_alternativos: list[SoatCodeCandidateStructured] = Field(default_factory=list, alias="p2")
    analisis_clinico: str = Field(default="", alias="ac")
    justificacion: str | None = Field(default=None, alias="j")
    fuente: str | None = Field(default=None, alias="f")
    error: str | None = Field(default=None, alias="er")


class SoatGeneratedCodeCollectionStructured(SoatStructuredModel):
    items: list[SoatGeneratedCodeStructured] = Field(default_factory=list, alias="it")
