"""Results from call simulation."""

from dataclasses import dataclass, field
from enum import Enum

from voiceground.events import VoicegroundEvent


class VoicegroundTerminationReason(str, Enum):
    """Reason why the simulation terminated."""

    UNKNOWN = "unknown"
    MAX_TURNS = "max_turns"
    TIMEOUT = "timeout"


@dataclass
class VoicegroundTranscriptEntry:
    """A single entry in the conversation transcript.

    Attributes:
        role: Either "user" (simulator) or "bot".
        text: The transcribed text.
        timestamp: Timestamp in seconds from simulation start.
    """

    role: str
    text: str
    timestamp: float


@dataclass
class VoicegroundSimulationResults:
    """Results from a completed simulation.

    Attributes:
        transcript: List of transcript entries in chronological order.
        events: All VoicegroundEvents captured during the simulation.
        turn_count: Number of completed conversation turns.
        duration_seconds: Total duration of the simulation.
        termination_reason: Why the simulation ended (max_turns or timeout).
    """

    transcript: list[VoicegroundTranscriptEntry] = field(default_factory=list)
    events: list[VoicegroundEvent] = field(default_factory=list)
    turn_count: int = 0
    duration_seconds: float = 0.0
    termination_reason: VoicegroundTerminationReason = VoicegroundTerminationReason.UNKNOWN

    @property
    def user_messages(self) -> list[str]:
        """Get all user (simulator) messages."""
        return [entry.text for entry in self.transcript if entry.role == "user"]

    @property
    def bot_messages(self) -> list[str]:
        """Get all bot messages."""
        return [entry.text for entry in self.transcript if entry.role == "bot"]

    def to_dict(self) -> dict:
        """Convert to a dictionary for serialization."""
        return {
            "transcript": [
                {"role": entry.role, "text": entry.text, "timestamp": entry.timestamp}
                for entry in self.transcript
            ],
            "turn_count": self.turn_count,
            "duration_seconds": self.duration_seconds,
            "termination_reason": self.termination_reason.value,
        }
