"""Analysis result schemas for slopit.

This module defines the result types produced by analyzers and pipelines.
"""

from typing import Literal

from pydantic import BaseModel, Field

from slopit.schemas.flags import AnalysisFlag
from slopit.schemas.types import JsonValue


class AnalysisResult(BaseModel):
    """Result from a single analyzer.

    Attributes
    ----------
    analyzer
        Name of the analyzer that produced this result.
    session_id
        ID of the analyzed session.
    trials
        Per-trial analysis results.
    flags
        Flags generated by the analyzer.
    session_summary
        Session-level summary statistics.
    """

    analyzer: str
    session_id: str
    trials: list[dict[str, JsonValue]] = Field(default_factory=lambda: list[dict[str, JsonValue]]())
    flags: list[AnalysisFlag] = Field(default_factory=lambda: list[AnalysisFlag]())
    session_summary: dict[str, JsonValue] = Field(default_factory=lambda: dict[str, JsonValue]())


class SessionVerdict(BaseModel):
    """Final verdict for a session.

    Attributes
    ----------
    status
        Overall status: "clean", "suspicious", or "flagged".
    confidence
        Confidence in the verdict (0.0 to 1.0).
    flags
        All flags contributing to the verdict.
    summary
        Human-readable summary.
    """

    status: Literal["clean", "suspicious", "flagged"]
    confidence: float = Field(ge=0.0, le=1.0)
    flags: list[AnalysisFlag] = Field(default_factory=lambda: list[AnalysisFlag]())
    summary: str


class PipelineResult(BaseModel):
    """Result from the analysis pipeline.

    Attributes
    ----------
    sessions
        List of analyzed session IDs.
    analyzer_results
        Results from each analyzer, keyed by analyzer name.
    aggregated_flags
        Aggregated flags per session.
    verdicts
        Final verdict per session.
    """

    sessions: list[str] = Field(default_factory=list)
    analyzer_results: dict[str, list[AnalysisResult]] = Field(default_factory=dict)
    aggregated_flags: dict[str, list[AnalysisFlag]] = Field(default_factory=dict)
    verdicts: dict[str, SessionVerdict] = Field(default_factory=dict)

    def to_dict(self) -> dict[str, JsonValue]:
        """Convert to dictionary for JSON serialization."""
        return self.model_dump()
