"""Chat retention settings endpoints. GET /assistant/retention — Get account retention settings PATCH /assistant/retention — Update retention settings (owner only) Note: Chat CRUD endpoints were removed — the frontend uses /ai-sessions/{id}/chat (unified_chat_service) for all chat operations. The /assistant prefix is kept for the retention settings to avoid a frontend URL change. """ from typing import Annotated, Optional from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from app.api.deps import get_current_active_user, get_db from app.models.user import User from app.models.account import Account from app.schemas.assistant_chat import ( RetentionSettingsResponse, RetentionSettingsUpdate, ) router = APIRouter(prefix="/assistant", tags=["assistant-chat"]) @router.get("/retention", response_model=RetentionSettingsResponse) async def get_retention_settings( current_user: Annotated[User, Depends(get_current_active_user)], db: Annotated[AsyncSession, Depends(get_db)], ): """Get account chat retention settings.""" result = await db.execute( select(Account).where(Account.id == current_user.account_id) ) account = result.scalar_one_or_none() if not account: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Account not found") return RetentionSettingsResponse( chat_retention_days=account.chat_retention_days, chat_retention_max_count=account.chat_retention_max_count, ) @router.patch("/retention", response_model=RetentionSettingsResponse) async def update_retention_settings( data: RetentionSettingsUpdate, current_user: Annotated[User, Depends(get_current_active_user)], db: Annotated[AsyncSession, Depends(get_db)], ): """Update account chat retention settings (account owner only).""" result = await db.execute( select(Account).where(Account.id == current_user.account_id) ) account = result.scalar_one_or_none() if not account: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Account not found") if account.owner_id != current_user.id and not current_user.is_super_admin: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Only the account owner can update retention settings", ) if data.chat_retention_days is not None: account.chat_retention_days = data.chat_retention_days if data.chat_retention_max_count is not None: account.chat_retention_max_count = data.chat_retention_max_count await db.commit() return RetentionSettingsResponse( chat_retention_days=account.chat_retention_days, chat_retention_max_count=account.chat_retention_max_count, )