"""Output schema for coverage check 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 CoverageConcern(BaseModel):
    """Single coverage concern identified by AI."""

    code: str
    message: str
    severity: Severity
    medical_necessity_score: float = Field(ge=0.0, le=1.0)


class CoverageCheckOutput(BaseModel):
    """Structured output from the coverage check AI chain."""

    concerns: list[CoverageConcern] = Field(default_factory=list)
    overall_medical_necessity: Literal["supported", "questionable", "unsupported"]
    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="coverage_check_ai",
            findings=[
                Finding(
                    code=c.code,
                    message=c.message,
                    severity=c.severity,
                    field_name="procedure_code",
                    suggestion="Review medical necessity documentation before submission.",
                    context={
                        "confidence": self.confidence,
                        "medical_necessity_score": c.medical_necessity_score,
                        "overall": self.overall_medical_necessity,
                        "reasoning": self.reasoning,
                    },
                )
                for c in self.concerns
            ],
        )
