from typing import List
from fastapi import APIRouter, Depends, HTTPException
from sqlmodel import Session, select
from database import get_session
from models import PerformanceNickname, User, Performance
from schemas import PerformanceNicknameCreate, PerformanceNicknameRead
from auth import get_current_user

router = APIRouter(prefix="/nicknames", tags=["nicknames"])

@router.post("/", response_model=PerformanceNicknameRead)
def suggest_nickname(
    nickname: PerformanceNicknameCreate,
    session: Session = Depends(get_session),
    current_user: User = Depends(get_current_user)
):
    # Verify performance exists
    perf = session.get(Performance, nickname.performance_id)
    if not perf:
        raise HTTPException(status_code=404, detail="Performance not found")
        
    db_nickname = PerformanceNickname.model_validate(nickname)
    db_nickname.suggested_by = current_user.id
    db_nickname.status = "pending" # Default to pending
    
    session.add(db_nickname)
    session.commit()
    session.refresh(db_nickname)
    return db_nickname

@router.get("/performance/{performance_id}", response_model=List[PerformanceNicknameRead])
def read_performance_nicknames(
    performance_id: int,
    session: Session = Depends(get_session)
):
    # Only show approved nicknames publicly
    nicknames = session.exec(
        select(PerformanceNickname)
        .where(PerformanceNickname.performance_id == performance_id)
        .where(PerformanceNickname.status == "approved")
    ).all()
    return nicknames

# Moderator endpoints would go here (approve/reject)
