"""Verify step state and verification result models for Phase 4: Verify"""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/models.ipynb.

# %% auto #0
__all__ = ['VerifyStepState', 'SegmentSample', 'VerificationResult', 'VerifyUrls']

# %% ../nbs/models.ipynb #verify-models-imports
from typing import Optional, List
from typing_extensions import TypedDict
from dataclasses import dataclass, field

# %% ../nbs/models.ipynb #verify-state
class VerifyStepState(TypedDict, total=False):
    """State for Phase 4: Verify."""
    
    document_id: str  # UUID of committed Document node (required)
    media_path: Optional[str]  # Source media path for display context

# %% ../nbs/models.ipynb #segment-sample
@dataclass
class SegmentSample:
    """Lightweight segment data for sample display."""
    
    index: int  # Segment index in document
    text: str  # Truncated text (~60 chars)
    start_time: Optional[float] = None  # Start time in seconds
    end_time: Optional[float] = None  # End time in seconds
    
    @property
    def duration(self) -> Optional[float]:  # Computed duration in seconds
        """Compute duration from start and end times."""
        if self.start_time is not None and self.end_time is not None:
            return self.end_time - self.start_time
        return None
    
    def to_dict(self) -> dict:  # Serializable dictionary
        """Convert to dictionary for serialization."""
        return {
            "index": self.index,
            "text": self.text,
            "start_time": self.start_time,
            "end_time": self.end_time,
        }
    
    @classmethod
    def from_dict(cls, data: dict) -> "SegmentSample":  # Reconstructed instance
        """Create from dictionary."""
        return cls(
            index=data["index"],
            text=data["text"],
            start_time=data.get("start_time"),
            end_time=data.get("end_time"),
        )

# %% ../nbs/models.ipynb #verification-result
@dataclass
class VerificationResult:
    """Complete verification data from graph database queries."""
    
    # Document info (from graph)
    document_id: str  # UUID of Document node
    document_title: str  # Document title
    document_media_type: str  # Media type ('audio', 'video', 'text')
    
    # Segment stats (computed)
    segment_count: int  # Total number of segments
    total_duration: float  # Total duration in seconds
    avg_segment_duration: float  # Average segment duration in seconds
    
    # Integrity checks - STARTS_WITH
    has_starts_with: bool  # Whether STARTS_WITH edge exists
    starts_with_count: int  # Number of STARTS_WITH edges (should be 1)
    
    # Integrity checks - NEXT chain
    next_chain_complete: bool  # Whether NEXT chain is complete
    next_count: int  # Number of NEXT edges (should be segment_count - 1)
    
    # Integrity checks - PART_OF
    part_of_complete: bool  # Whether all PART_OF edges exist
    part_of_count: int  # Number of PART_OF edges (should be segment_count)
    
    # Integrity checks - timing
    all_have_timing: bool  # Whether all segments have timing
    segments_missing_timing: int  # Count of segments without timing
    
    # Integrity checks - sources
    all_have_sources: bool  # Whether all segments have sources
    segments_missing_sources: int  # Count of segments without sources
    
    # Source info
    source_plugins: List[str] = field(default_factory=list)  # Unique plugin names
    
    # Sample segments
    first_segments: List[SegmentSample] = field(default_factory=list)  # First 3 segments
    last_segments: List[SegmentSample] = field(default_factory=list)  # Last 3 segments
    
    @property
    def all_checks_passed(self) -> bool:  # Whether all integrity checks passed
        """Check if all integrity checks passed."""
        return (
            self.has_starts_with and
            self.starts_with_count == 1 and
            self.next_chain_complete and
            self.part_of_complete and
            self.all_have_timing and
            self.all_have_sources
        )
    
    def to_dict(self) -> dict:  # Serializable dictionary
        """Convert to dictionary for serialization."""
        return {
            "document_id": self.document_id,
            "document_title": self.document_title,
            "document_media_type": self.document_media_type,
            "segment_count": self.segment_count,
            "total_duration": self.total_duration,
            "avg_segment_duration": self.avg_segment_duration,
            "has_starts_with": self.has_starts_with,
            "starts_with_count": self.starts_with_count,
            "next_chain_complete": self.next_chain_complete,
            "next_count": self.next_count,
            "part_of_complete": self.part_of_complete,
            "part_of_count": self.part_of_count,
            "all_have_timing": self.all_have_timing,
            "segments_missing_timing": self.segments_missing_timing,
            "all_have_sources": self.all_have_sources,
            "segments_missing_sources": self.segments_missing_sources,
            "source_plugins": self.source_plugins,
            "first_segments": [s.to_dict() for s in self.first_segments],
            "last_segments": [s.to_dict() for s in self.last_segments],
        }
    
    @classmethod
    def from_dict(cls, data: dict) -> "VerificationResult":  # Reconstructed instance
        """Create from dictionary."""
        return cls(
            document_id=data["document_id"],
            document_title=data["document_title"],
            document_media_type=data["document_media_type"],
            segment_count=data["segment_count"],
            total_duration=data["total_duration"],
            avg_segment_duration=data["avg_segment_duration"],
            has_starts_with=data["has_starts_with"],
            starts_with_count=data["starts_with_count"],
            next_chain_complete=data["next_chain_complete"],
            next_count=data["next_count"],
            part_of_complete=data["part_of_complete"],
            part_of_count=data["part_of_count"],
            all_have_timing=data["all_have_timing"],
            segments_missing_timing=data["segments_missing_timing"],
            all_have_sources=data["all_have_sources"],
            segments_missing_sources=data["segments_missing_sources"],
            source_plugins=data.get("source_plugins", []),
            first_segments=[SegmentSample.from_dict(s) for s in data.get("first_segments", [])],
            last_segments=[SegmentSample.from_dict(s) for s in data.get("last_segments", [])],
        )

# %% ../nbs/models.ipynb #verify-urls
@dataclass
class VerifyUrls:
    """URL bundle for Phase 4 verify route handlers."""
    
    # Verification routes
    verify: str = ""  # Main verification computation route
    sample: str = ""  # Jump-to-index sample fetch route
