"""Output schema for prior auth interpreter AI chain."""
from __future__ import annotations

from pydantic import BaseModel, Field

from claim_validator.constants import Severity
from claim_validator.models.results import Finding, ValidatorOutput


class PADecision(BaseModel):
    """Parsed prior auth decision from the payer response."""

    approved: bool
    auth_number: str | None = None
    conditions: list[str] = Field(default_factory=list)
    denial_reason: str | None = None


class PAInterpreterOutput(BaseModel):
    """Structured output from the PA interpreter AI chain."""

    decision: PADecision
    next_steps: list[str] = Field(default_factory=list)
    reasoning: str

    def to_validator_output(self) -> ValidatorOutput:
        """Convert to the standard ValidatorOutput used by the pipeline."""
        findings: list[Finding] = []
        if not self.decision.approved and self.decision.denial_reason:
            findings.append(
                Finding(
                    code="AI_PA_DENIAL_EXPLAINED",
                    message=self.decision.denial_reason,
                    severity=Severity.WARNING,
                    field_name="",
                    suggestion="; ".join(self.next_steps) if self.next_steps else "",
                )
            )
        return ValidatorOutput(validator_name="pa_interpreter_ai", findings=findings)
