"""admin/routers/qa.py — Custom Q&A training data routes. Routes: GET /{slug}/qa POST /{slug}/qa POST /{slug}/qa/import GET /{slug}/qa/export PUT /{slug}/qa/{qa_id} DELETE /{slug}/qa/{qa_id} """ from fastapi import APIRouter, Depends, File, UploadFile from sqlalchemy.ext.asyncio import AsyncSession from app.dependencies import get_db, get_current_author_scoped from app.schemas.admin import QACreateRequest, QAUpdateRequest from app.services.qa_service import QAService router = APIRouter() @router.get("/{author_slug}/qa") async def list_qa( author_slug: str, book_id: str | None = None, current_user=Depends(get_current_author_scoped), db: AsyncSession = Depends(get_db), ): """List all custom Q&A pairs for the author.""" return await QAService(db).list_qa(current_user.id, book_id) @router.post("/{author_slug}/qa", status_code=201) async def create_qa( author_slug: str, body: QACreateRequest, current_user=Depends(get_current_author_scoped), db: AsyncSession = Depends(get_db), ): """Create a new custom Q&A pair. R-029: Validated via Pydantic schema.""" return await QAService(db).create_qa(current_user.id, body) @router.post("/{author_slug}/qa/import") async def import_qa_csv( author_slug: str, current_user=Depends(get_current_author_scoped), db: AsyncSession = Depends(get_db), file: UploadFile = File(...), ): """Bulk import Q&A pairs from CSV (columns: question,answer,category,priority).""" return await QAService(db).import_qa_csv(current_user.id, file) @router.get("/{author_slug}/qa/export") async def export_qa_csv( author_slug: str, current_user=Depends(get_current_author_scoped), db: AsyncSession = Depends(get_db), ): """Export all Q&A pairs as CSV.""" return await QAService(db).export_qa_csv(current_user.id) @router.put("/{author_slug}/qa/{qa_id}") async def update_qa( author_slug: str, qa_id: str, body: QAUpdateRequest, current_user=Depends(get_current_author_scoped), db: AsyncSession = Depends(get_db), ): """Update an existing Q&A pair. R-029: Validated via Pydantic schema.""" return await QAService(db).update_qa(current_user.id, qa_id, body) @router.delete("/{author_slug}/qa/{qa_id}") async def delete_qa( author_slug: str, qa_id: str, current_user=Depends(get_current_author_scoped), db: AsyncSession = Depends(get_db), ): """Delete a Q&A pair.""" return await QAService(db).delete_qa(current_user.id, qa_id)