"""
Classroom API - Template, Homework, Material Pydantic Models.
"""

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


# === Template Models ===

class TemplateCreate(BaseModel):
    """Request zum Erstellen einer Vorlage."""
    name: str = Field(..., min_length=1, max_length=200, description="Name der Vorlage")
    description: str = Field("", max_length=1000, description="Beschreibung")
    subject: str = Field("", max_length=100, description="Fach")
    grade_level: str = Field("", max_length=50, description="Klassenstufe (z.B. '7', '10')")
    phase_durations: Optional[Dict[str, int]] = Field(
        None,
        description="Phasendauern in Minuten"
    )
    default_topic: str = Field("", max_length=500, description="Vorausgefuelltes Thema")
    default_notes: str = Field("", description="Vorausgefuellte Notizen")
    is_public: bool = Field(False, description="Vorlage fuer alle sichtbar?")


class TemplateUpdate(BaseModel):
    """Request zum Aktualisieren einer Vorlage."""
    name: Optional[str] = Field(None, min_length=1, max_length=200)
    description: Optional[str] = Field(None, max_length=1000)
    subject: Optional[str] = Field(None, max_length=100)
    grade_level: Optional[str] = Field(None, max_length=50)
    phase_durations: Optional[Dict[str, int]] = None
    default_topic: Optional[str] = Field(None, max_length=500)
    default_notes: Optional[str] = None
    is_public: Optional[bool] = None


class TemplateResponse(BaseModel):
    """Response fuer eine einzelne Vorlage."""
    template_id: str
    teacher_id: str
    name: str
    description: str
    subject: str
    grade_level: str
    phase_durations: Dict[str, int]
    default_topic: str
    default_notes: str
    is_public: bool
    usage_count: int
    total_duration_minutes: int
    created_at: Optional[str]
    updated_at: Optional[str]
    is_system_template: bool = False


class TemplateListResponse(BaseModel):
    """Response fuer Template-Liste."""
    templates: List[TemplateResponse]
    total_count: int


# === Homework Models ===

class CreateHomeworkRequest(BaseModel):
    """Request zum Erstellen einer Hausaufgabe."""
    teacher_id: str
    class_id: str
    subject: str
    title: str = Field(..., max_length=300)
    description: str = ""
    session_id: Optional[str] = None
    due_date: Optional[str] = Field(None, description="ISO-Format Datum")


class UpdateHomeworkRequest(BaseModel):
    """Request zum Aktualisieren einer Hausaufgabe."""
    title: Optional[str] = Field(None, max_length=300)
    description: Optional[str] = None
    due_date: Optional[str] = Field(None, description="ISO-Format Datum")
    status: Optional[str] = Field(None, description="assigned, in_progress, completed")


class HomeworkResponse(BaseModel):
    """Response fuer eine Hausaufgabe."""
    homework_id: str
    teacher_id: str
    class_id: str
    subject: str
    title: str
    description: str
    session_id: Optional[str]
    due_date: Optional[str]
    status: str
    is_overdue: bool
    created_at: Optional[str]
    updated_at: Optional[str]


class HomeworkListResponse(BaseModel):
    """Response fuer Liste von Hausaufgaben."""
    homework: List[HomeworkResponse]
    total: int


# === Material Models ===

class CreateMaterialRequest(BaseModel):
    """Request zum Erstellen eines Materials."""
    teacher_id: str
    title: str = Field(..., max_length=300)
    material_type: str = Field("document", description="document, link, video, image, worksheet, presentation, other")
    url: Optional[str] = Field(None, max_length=2000)
    description: str = ""
    phase: Optional[str] = Field(None, description="einstieg, erarbeitung, sicherung, transfer, reflexion")
    subject: str = ""
    grade_level: str = ""
    tags: List[str] = []
    is_public: bool = False
    session_id: Optional[str] = None


class UpdateMaterialRequest(BaseModel):
    """Request zum Aktualisieren eines Materials."""
    title: Optional[str] = Field(None, max_length=300)
    material_type: Optional[str] = None
    url: Optional[str] = Field(None, max_length=2000)
    description: Optional[str] = None
    phase: Optional[str] = None
    subject: Optional[str] = None
    grade_level: Optional[str] = None
    tags: Optional[List[str]] = None
    is_public: Optional[bool] = None


class MaterialResponse(BaseModel):
    """Response fuer ein Material."""
    material_id: str
    teacher_id: str
    title: str
    material_type: str
    url: Optional[str]
    description: str
    phase: Optional[str]
    subject: str
    grade_level: str
    tags: List[str]
    is_public: bool
    usage_count: int
    session_id: Optional[str]
    created_at: Optional[str]
    updated_at: Optional[str]


class MaterialListResponse(BaseModel):
    """Response fuer Liste von Materialien."""
    materials: List[MaterialResponse]
    total: int
