"""N10-N15: conversations between players and coaches.""" from typing import Annotated from uuid import UUID from fastapi import APIRouter, Depends, Header, Query, Response, status from app.common.academy_access import CurrentStaff from app.common.schemas import Page from app.core.dependencies import CurrentAccount, Db, Paging, Storage from app.integrations.push import PushClient, get_push_client from app.modules.idempotency.service import run_idempotent from app.modules.threads.schemas import EnsureThreadIn, MessageIn, ReadIn, SendResult, ThreadEnvelope, ThreadOut from app.modules.threads.service import ThreadService router = APIRouter(prefix="/v1", tags=["threads"]) def get_thread_service(db: Db, storage: Storage, push: Annotated[PushClient, Depends(get_push_client)]) -> ThreadService: return ThreadService(db, storage, push) Service = Annotated[ThreadService, Depends(get_thread_service)] @router.get("/threads", response_model=Page[ThreadOut]) async def list_threads(account: CurrentAccount, service: Service, paging: Paging, mode: Annotated[ str | None, Query(pattern="^[hp]$")] = None, player_id: UUID | None = None, unread: bool | None = None) -> Page[ThreadOut]: return await service.list_threads(account, paging, mode=mode, player_id=player_id, unread=unread) @router.get("/threads/{thread_id}", response_model=ThreadEnvelope) async def get_thread(thread_id: UUID, account: CurrentAccount, service: Service, message_cursor: str | None = None, message_limit: Annotated[int, Query(ge=1, le=100)] = 50) -> ThreadEnvelope: return ThreadEnvelope(thread=await service.get(account, thread_id, message_cursor, message_limit)) @router.get("/sessions/{session_id}/thread", response_model=ThreadEnvelope) async def session_thread(session_id: UUID, account: CurrentAccount, service: Service) -> ThreadEnvelope: return ThreadEnvelope(thread=await service.for_session(account, session_id)) @router.post("/threads/{thread_id}/read", status_code=status.HTTP_204_NO_CONTENT) async def mark_read(thread_id: UUID, body: ReadIn, account: CurrentAccount, service: Service) -> Response: await service.mark_read(account, thread_id, body.last_message_id) return Response(status_code=status.HTTP_204_NO_CONTENT) @router.post("/threads/{thread_id}/messages", status_code=status.HTTP_201_CREATED, response_model=SendResult) async def send_message(thread_id: UUID, body: MessageIn, account: CurrentAccount, db: Db, service: Service, idempotency_key: Annotated[str | None, Header()] = None): # The key is scoped to the account, so the thread id is part of what must match on replay. return await run_idempotent(db, account.id, idempotency_key, body, lambda: service.send(account, thread_id, body), status_code=status.HTTP_201_CREATED, scope=thread_id) @router.post("/coach/players/{player_id}/thread", status_code=status.HTTP_201_CREATED, response_model=ThreadEnvelope) async def ensure_thread(player_id: UUID, body: EnsureThreadIn, staff: CurrentStaff, response: Response, service: Service) -> ThreadEnvelope: thread, created = await service.ensure_general(staff, player_id, body) if not created: response.status_code = status.HTTP_200_OK return ThreadEnvelope(thread=thread)