from fastapi import APIRouter, Depends, HTTPException

from app.schemas import SessionRouteRequest
from app.auth import require_user
from app.dependencies import get_store
from app.store import User, SessionOwnershipError

router = APIRouter()


@router.post("/sessions/{session_id}/route")
async def set_session_route(
    session_id: str,
    payload: SessionRouteRequest,
    user: User = Depends(require_user),
):
    try:
        session = get_store().update_session(session_id, user.id, payload.model_dump())
    except SessionOwnershipError as exc:
        raise HTTPException(status_code=403, detail=str(exc))
    return {"session_id": session_id, "route": session.data}
