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

from typing import Literal

from pydantic import BaseModel, Field

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


class PriorAuthRequirement(BaseModel):
    """Single procedure's prior authorization requirement."""

    procedure_code: str
    requires_auth: bool
    reason: str
    urgency: Literal["routine", "urgent", "emergent"] = "routine"


class PriorAuthOutput(BaseModel):
    """Structured output from the prior auth AI chain."""

    requirements: list[PriorAuthRequirement] = Field(default_factory=list)
    overall_determination: Literal["required", "not_required", "uncertain"]
    confidence: float = Field(ge=0.0, le=1.0, description="Overall confidence score")
    reasoning: str

    def to_validator_output(self) -> ValidatorOutput:
        """Convert to the standard ValidatorOutput used by the pipeline."""
        return ValidatorOutput(
            validator_name="prior_auth_ai",
            findings=[
                Finding(
                    code="AI_PRIOR_AUTH_LIKELY",
                    message=f"{r.procedure_code}: {r.reason}",
                    severity=Severity.WARNING,
                    field_name="procedure_code",
                    suggestion="Verify prior authorization status with the payer before submission.",
                    context={
                        "confidence": self.confidence,
                        "requires_auth": r.requires_auth,
                        "urgency": r.urgency,
                        "overall": self.overall_determination,
                        "reasoning": self.reasoning,
                    },
                )
                for r in self.requirements
                if r.requires_auth
            ],
        )
