"""System settings routes — get and upsert (admin only).""" from __future__ import annotations import uuid from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.ext.asyncio import AsyncSession from app.core.auth import check_permission from app.core.db import get_db from app.deps import get_current_user from app.schemas.system_settings import SystemSettingsUpsert from app.services import system_settings_service router = APIRouter(prefix="/api/v1/system-settings", tags=["system-settings"]) @router.get("") async def get_system_settings( db: AsyncSession = Depends(get_db), current_user: dict = Depends(get_current_user), ): """Get system settings for the current tenant.""" tenant_id = uuid.UUID(current_user["tenant_id"]) result = await system_settings_service.get_system_settings(db, tenant_id) if result is None: return {} return result @router.put("") async def upsert_system_settings( body: SystemSettingsUpsert, db: AsyncSession = Depends(get_db), current_user: dict = Depends(get_current_user), ): """Upsert system settings. Requires admin permission.""" tenant_id = uuid.UUID(current_user["tenant_id"]) user_id = uuid.UUID(current_user["user_id"]) role = current_user.get("role", "viewer") if not check_permission(role, "settings", "update"): raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail={"detail": "Insufficient permissions", "code": "forbidden"}, ) data = body.model_dump() return await system_settings_service.upsert_system_settings(db, tenant_id, user_id, data)