from typing import Literal

from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session

from auth import get_current_user
from database import get_db
from models import User
from schemas import CoachSessionResponse, CoachTurnRequest, CoachTurnResponse
from services.memory_coach_service import memory_coach_service

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


@router.get("/session", response_model=CoachSessionResponse)
def coach_session(
    track: Literal["accumulation", "book"] = Query("accumulation"),
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user),
):
    return memory_coach_service.start_session(db, current_user, track=track)


@router.post("/turn", response_model=CoachTurnResponse)
def coach_turn(
    data: CoachTurnRequest,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user),
):
    return memory_coach_service.handle_turn(
        db,
        current_user,
        data.word_id,
        data.stage,
        data.user_message,
        data.hints_used,
        data.duration_seconds or 0,
    )
