"""
Klausur-Service Request Models

Pydantic models for API request/response validation.
"""

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

from .enums import KlausurModus


# =============================================
# KLAUSUR REQUESTS
# =============================================

class KlausurCreate(BaseModel):
    """Request to create a new Klausur."""
    title: str
    subject: str
    modus: KlausurModus = KlausurModus.VORABITUR
    class_id: Optional[str] = None
    year: int = 2025
    semester: str = "Q1"


class KlausurUpdate(BaseModel):
    """Request to update a Klausur."""
    title: Optional[str] = None
    subject: Optional[str] = None
    erwartungshorizont: Optional[Dict[str, Any]] = None


# =============================================
# STUDENT REQUESTS
# =============================================

class StudentUpload(BaseModel):
    """Request for student work upload metadata."""
    student_name: str
    student_id: Optional[str] = None


# =============================================
# GRADING REQUESTS
# =============================================

class CriterionScoreUpdate(BaseModel):
    """Request to update a criterion score."""
    criterion: str
    score: int
    annotations: Optional[List[str]] = None


class GutachtenUpdate(BaseModel):
    """Request to update the Gutachten."""
    einleitung: str
    hauptteil: str
    fazit: str
    staerken: Optional[List[str]] = None
    schwaechen: Optional[List[str]] = None


class GutachtenGenerateRequest(BaseModel):
    """Request to generate a KI-based Gutachten."""
    include_strengths: bool = True
    include_weaknesses: bool = True
    tone: str = "formal"  # formal, friendly, constructive
    # BYOEH RAG Integration
    use_eh: bool = False  # Whether to use Erwartungshorizont for context
    eh_passphrase: Optional[str] = None  # Passphrase for EH decryption


# =============================================
# EXAMINER REQUESTS
# =============================================

class ExaminerAssignment(BaseModel):
    """Request to assign an examiner."""
    examiner_id: str
    examiner_role: str  # first_examiner, second_examiner
    notes: Optional[str] = None


class ExaminerResult(BaseModel):
    """Request to submit an examiner's result."""
    grade_points: int
    notes: Optional[str] = None


# =============================================
# BYOEH REQUESTS
# =============================================

class EHMetadata(BaseModel):
    """Metadata for an Erwartungshorizont."""
    title: str
    subject: str
    niveau: str  # 'eA' | 'gA'
    year: int
    aufgaben_nummer: Optional[str] = None


class EHUploadMetadata(BaseModel):
    """Metadata for EH upload including encryption info."""
    metadata: EHMetadata
    encryption_key_hash: str
    salt: str
    rights_confirmed: bool
    original_filename: str


class EHRAGQuery(BaseModel):
    """Request for RAG query against Erwartungshorizonte."""
    query_text: str
    passphrase: str
    subject: Optional[str] = None
    limit: int = 5


class EHIndexRequest(BaseModel):
    """Request to index an Erwartungshorizont."""
    passphrase: str


class EHShareRequest(BaseModel):
    """Request to share EH with another examiner."""
    user_id: str  # User to share with
    role: str  # second_examiner, third_examiner, supervisor
    encrypted_passphrase: str  # Passphrase encrypted for recipient
    passphrase_hint: Optional[str] = None
    klausur_id: Optional[str] = None  # Optional: link to specific Klausur


class EHLinkKlausurRequest(BaseModel):
    """Request to link EH to a Klausur."""
    klausur_id: str


class EHInviteRequest(BaseModel):
    """Request to invite another user to access an EH."""
    invitee_email: str  # Email of the recipient
    invitee_id: Optional[str] = None  # User ID if known
    role: str  # second_examiner, third_examiner, supervisor, department_head
    klausur_id: Optional[str] = None  # Optional: link to specific Klausur
    message: Optional[str] = None  # Optional message for the invitee
    expires_in_days: int = 14  # Default 14 days expiration


class EHAcceptInviteRequest(BaseModel):
    """Request to accept an invitation and receive the encrypted passphrase."""
    encrypted_passphrase: str  # The passphrase encrypted for the invitee
