from fastapi import APIRouter, Depends, HTTPException from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from app.models.database import get_session from app.models.orm import Clue, Vote, Character from app.models.schemas import ClueCreate, ClueResponse, ClueUnlockRequest, VoteCreate, VoteResponse router = APIRouter() @router.get("/{script_id}/clues", response_model=list[ClueResponse]) async def list_clues(script_id: str, session: AsyncSession = Depends(get_session)): result = await session.execute(select(Clue).where(Clue.script_id == script_id)) return list(result.scalars().all()) @router.post("/{script_id}/clues", response_model=ClueResponse) async def create_clue(script_id: str, req: ClueCreate, session: AsyncSession = Depends(get_session)): clue = Clue( script_id=script_id, name=req.name, content=req.content, clue_type=req.clue_type, owner_id=req.owner_id, phase=req.phase, visibility=req.visibility, visible_to=req.visible_to, ) session.add(clue) await session.commit() await session.refresh(clue) return clue @router.post("/clues/{clue_id}/unlock") async def unlock_clue( clue_id: str, req: ClueUnlockRequest, session: AsyncSession = Depends(get_session), ): result = await session.execute(select(Clue).where(Clue.id == clue_id)) clue = result.scalars().first() if not clue: raise HTTPException(status_code=404, detail="Clue not found") from datetime import datetime clue.is_unlocked = True clue.unlocked_by = req.character_id clue.unlocked_at = datetime.utcnow() await session.commit() await session.refresh(clue) return {"ok": True, "clue": ClueResponse.model_validate(clue)} @router.delete("/clues/{clue_id}") async def delete_clue(clue_id: str, session: AsyncSession = Depends(get_session)): result = await session.execute(select(Clue).where(Clue.id == clue_id)) clue = result.scalars().first() if not clue: raise HTTPException(status_code=404, detail="Clue not found") await session.delete(clue) await session.commit() return {"ok": True} @router.get("/votes", response_model=list[VoteResponse]) async def list_votes(round_number: int = 1, session: AsyncSession = Depends(get_session)): result = await session.execute(select(Vote).where(Vote.round_number == round_number)) return list(result.scalars().all()) @router.post("/votes", response_model=VoteResponse) async def create_vote(req: VoteCreate, session: AsyncSession = Depends(get_session)): vote = Vote( voter_id=req.voter_id, target_id=req.target_id, reason=req.reason, ) session.add(vote) await session.commit() await session.refresh(vote) return vote