"""
Klausur-Service Exam Routes

CRUD endpoints for Klausuren.
"""

import uuid
from datetime import datetime, timezone

from fastapi import APIRouter, HTTPException, Request

from models.exam import Klausur
from models.requests import KlausurCreate, KlausurUpdate
from services.auth_service import get_current_user
import storage

router = APIRouter()


@router.get("/api/v1/klausuren")
async def list_klausuren(request: Request):
    """List all Klausuren for current teacher."""
    user = get_current_user(request)
    user_klausuren = [
        k.to_dict() for k in storage.klausuren_db.values()
        if k.teacher_id == user["user_id"]
    ]
    return user_klausuren


@router.post("/api/v1/klausuren")
async def create_klausur(data: KlausurCreate, request: Request):
    """Create a new Klausur."""
    user = get_current_user(request)

    klausur = Klausur(
        id=str(uuid.uuid4()),
        title=data.title,
        subject=data.subject,
        modus=data.modus,
        class_id=data.class_id,
        year=data.year,
        semester=data.semester,
        erwartungshorizont=None,
        students=[],
        created_at=datetime.now(timezone.utc),
        teacher_id=user["user_id"]
    )

    storage.klausuren_db[klausur.id] = klausur
    return klausur.to_dict()


@router.get("/api/v1/klausuren/{klausur_id}")
async def get_klausur(klausur_id: str, request: Request):
    """Get a specific Klausur."""
    user = get_current_user(request)

    if klausur_id not in storage.klausuren_db:
        raise HTTPException(status_code=404, detail="Klausur not found")

    klausur = storage.klausuren_db[klausur_id]
    if klausur.teacher_id != user["user_id"] and user.get("role") != "admin":
        raise HTTPException(status_code=403, detail="Access denied")

    return klausur.to_dict()


@router.put("/api/v1/klausuren/{klausur_id}")
async def update_klausur(klausur_id: str, data: KlausurUpdate, request: Request):
    """Update a Klausur."""
    user = get_current_user(request)

    if klausur_id not in storage.klausuren_db:
        raise HTTPException(status_code=404, detail="Klausur not found")

    klausur = storage.klausuren_db[klausur_id]
    if klausur.teacher_id != user["user_id"] and user.get("role") != "admin":
        raise HTTPException(status_code=403, detail="Access denied")

    if data.title:
        klausur.title = data.title
    if data.subject:
        klausur.subject = data.subject
    if data.erwartungshorizont:
        klausur.erwartungshorizont = data.erwartungshorizont

    return klausur.to_dict()


@router.delete("/api/v1/klausuren/{klausur_id}")
async def delete_klausur(klausur_id: str, request: Request):
    """Delete a Klausur and all associated student work."""
    user = get_current_user(request)

    if klausur_id not in storage.klausuren_db:
        raise HTTPException(status_code=404, detail="Klausur not found")

    klausur = storage.klausuren_db[klausur_id]
    if klausur.teacher_id != user["user_id"] and user.get("role") != "admin":
        raise HTTPException(status_code=403, detail="Access denied")

    # Remove student records
    for student in klausur.students:
        if student.id in storage.students_db:
            del storage.students_db[student.id]

    del storage.klausuren_db[klausur_id]
    return {"success": True}
