"""
Classroom API - Session & Phase Pydantic Models.
"""

from typing import Dict, List, Optional, Any
from pydantic import BaseModel, Field


# === Session Models ===

class CreateSessionRequest(BaseModel):
    """Request zum Erstellen einer neuen Session."""
    teacher_id: str = Field(..., description="ID des Lehrers")
    class_id: str = Field(..., description="ID der Klasse")
    subject: str = Field(..., description="Unterrichtsfach")
    topic: Optional[str] = Field(None, description="Thema der Stunde")
    phase_durations: Optional[Dict[str, int]] = Field(
        None,
        description="Optionale individuelle Phasendauern in Minuten"
    )


class NotesRequest(BaseModel):
    """Request zum Aktualisieren von Notizen."""
    notes: str = Field("", description="Stundennotizen")
    homework: str = Field("", description="Hausaufgaben")


class ExtendTimeRequest(BaseModel):
    """Request zum Verlaengern der aktuellen Phase (Feature f28)."""
    minutes: int = Field(5, ge=1, le=30, description="Zusaetzliche Minuten (1-30)")


class PhaseInfo(BaseModel):
    """Informationen zu einer Phase."""
    phase: str
    display_name: str
    icon: str
    duration_minutes: int
    is_completed: bool
    is_current: bool
    is_future: bool


class TimerStatus(BaseModel):
    """Timer-Status einer Phase."""
    remaining_seconds: int
    remaining_formatted: str
    total_seconds: int
    total_formatted: str
    elapsed_seconds: int
    elapsed_formatted: str
    percentage_remaining: int
    percentage_elapsed: int
    percentage: int = Field(description="Alias fuer percentage_remaining (Visual Timer)")
    warning: bool
    overtime: bool
    overtime_seconds: int
    overtime_formatted: Optional[str]
    is_paused: bool = Field(False, description="Ist der Timer pausiert?")


class SuggestionItem(BaseModel):
    """Ein Aktivitaets-Vorschlag."""
    id: str
    title: str
    description: str
    activity_type: str
    estimated_minutes: int
    icon: str
    content_url: Optional[str]


class SessionResponse(BaseModel):
    """Vollstaendige Session-Response."""
    session_id: str
    teacher_id: str
    class_id: str
    subject: str
    topic: Optional[str]
    current_phase: str
    phase_display_name: str
    phase_started_at: Optional[str]
    lesson_started_at: Optional[str]
    lesson_ended_at: Optional[str]
    timer: TimerStatus
    phases: List[PhaseInfo]
    phase_history: List[Dict[str, Any]]
    notes: str
    homework: str
    is_active: bool
    is_ended: bool
    is_paused: bool = Field(False, description="Ist die Stunde pausiert?")


class SuggestionsResponse(BaseModel):
    """Response fuer Vorschlaege."""
    suggestions: List[SuggestionItem]
    current_phase: str
    phase_display_name: str
    total_available: int


class PhasesListResponse(BaseModel):
    """Liste aller verfuegbaren Phasen."""
    phases: List[Dict[str, Any]]


class ActiveSessionsResponse(BaseModel):
    """Liste aktiver Sessions."""
    sessions: List[Dict[str, Any]]
    count: int


# === Session History Models ===

class SessionHistoryItem(BaseModel):
    """Einzelner Eintrag in der Session-History."""
    session_id: str
    teacher_id: str
    class_id: str
    subject: str
    topic: Optional[str]
    lesson_started_at: Optional[str]
    lesson_ended_at: Optional[str]
    total_duration_minutes: Optional[int]
    phases_completed: int
    notes: str
    homework: str


class SessionHistoryResponse(BaseModel):
    """Response fuer Session-History."""
    sessions: List[SessionHistoryItem]
    total_count: int
    limit: int
    offset: int
