"""review.json schema produced by the Code Reviewer."""

from __future__ import annotations

from typing import List, Literal, Optional

from pydantic import BaseModel, Field, field_validator

BLOCKING_SEVERITIES = frozenset({"critical", "high"})


class ReviewFinding(BaseModel):
    severity: Literal["critical", "high", "medium", "low", "info"]
    category: str
    file: Optional[str] = None
    line: Optional[int] = None
    message: str
    fixable: bool = True

    @field_validator("severity", mode="before")
    @classmethod
    def normalize_severity(cls, value: object) -> object:
        """OpenAI often returns 'High' / 'Medium'; accept case-insensitive values."""
        if isinstance(value, str):
            return value.strip().lower()
        return value


class ReviewArtifact(BaseModel):
    jira_key: str
    passed: bool
    cycle: int
    findings: List[ReviewFinding] = Field(default_factory=list)
    summary: str
    static_analysis_status: str = "skipped"
    notes: Optional[str] = None

    def blocking_findings(self) -> List[ReviewFinding]:
        return [f for f in self.findings if f.severity in BLOCKING_SEVERITIES]

    def has_fixable_blocking(self) -> bool:
        return any(f.fixable for f in self.blocking_findings())
