from pydantic import BaseModel, Field, field_validator


class JudgeOutput(BaseModel):
    """Structured verdict from the objective-evaluation agent."""

    achieved: bool = Field(description="Whether the objective was substantially fulfilled")
    score: float = Field(ge=0.0, le=1.0, description="Score from 0.0 to 1.0")
    reason: str = Field(max_length=500, description="Brief explanation for the score")

    @field_validator("achieved", mode="before")
    @classmethod
    def coerce_achieved(cls, v: object) -> bool:
        if isinstance(v, str):
            return v.strip().lower() in ("true", "yes", "achieved", "1")
        return bool(v)

    @field_validator("score", mode="before")
    @classmethod
    def coerce_score(cls, v: object) -> float:
        try:
            return max(0.0, min(1.0, float(v)))  # type: ignore[arg-type]
        except (TypeError, ValueError):
            return 0.0


class GoalResult(BaseModel):
    achieved: bool
    score: float
    reason: str
    goal_name: str
