"""Output schema for code validation 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 CodeValidationFinding(BaseModel):
    """Single finding from AI code validation."""

    code: str = Field(description="Finding code e.g. 'AI_CLINICAL_IMPLAUSIBILITY'")
    message: str = Field(description="Human-readable description of the issue")
    severity: Severity = Field(description="ERROR, WARNING, or INFO")
    field_name: str = Field(description="Field that triggered this finding")
    line_number: int | None = Field(default=None, description="Claim line number if applicable")
    suggestion: str = Field(default="", description="Recommended fix")


class CodeValidationOutput(BaseModel):
    """Structured output from the code validation AI chain."""

    findings: list[CodeValidationFinding] = Field(default_factory=list)
    confidence: float = Field(ge=0.0, le=1.0, description="Overall confidence score")
    reasoning: str = Field(description="Brief clinical reasoning summary")

    def to_validator_output(self) -> ValidatorOutput:
        """Convert to the standard ValidatorOutput used by the pipeline."""
        return ValidatorOutput(
            validator_name="code_validation_ai",
            findings=[
                Finding(
                    code=f.code,
                    message=f.message,
                    severity=f.severity,
                    field_name=f.field_name,
                    line_number=f.line_number,
                    suggestion=f.suggestion,
                    context={"confidence": self.confidence, "reasoning": self.reasoning},
                )
                for f in self.findings
            ],
        )
