"""Data Transfer Objects for speaker diarization.

These DTOs define the data structures used by diarization components.
"""

from dataclasses import dataclass


@dataclass(frozen=True)
class SpeakerTurn:
    """Speaker turn from diarization output.

    Represents a time segment where a specific speaker is talking.
    """

    speaker: str  # Speaker label (e.g., "SPEAKER_00")
    start: float  # Start time in seconds
    end: float  # End time in seconds
    confidence: float = 1.0  # Confidence score (0.0-1.0)

    def __post_init__(self) -> None:
        """Validate turn data."""
        if self.end < self.start:
            raise ValueError(f"Turn end ({self.end}) < start ({self.start})")
        if not 0.0 <= self.confidence <= 1.0:
            raise ValueError(f"Confidence must be 0.0-1.0, got {self.confidence}")

    @property
    def duration(self) -> float:
        """Duration of the turn in seconds."""
        return self.end - self.start

    def overlaps(self, start: float, end: float) -> bool:
        """Check if this turn overlaps with a time range.

        Args:
            start: Range start time in seconds.
            end: Range end time in seconds.

        Returns:
            True if there is any overlap.
        """
        return self.start < end and self.end > start

    def overlap_duration(self, start: float, end: float) -> float:
        """Calculate overlap duration with a time range.

        Args:
            start: Range start time in seconds.
            end: Range end time in seconds.

        Returns:
            Overlap duration in seconds (0.0 if no overlap).
        """
        overlap_start = max(self.start, start)
        overlap_end = min(self.end, end)
        return max(0.0, overlap_end - overlap_start)
