""" Superadmin endpoint for managing default device configuration. """ import json from pathlib import Path from typing import Annotated from fastapi import APIRouter, Depends, HTTPException, status from pydantic import BaseModel from app.api.deps import get_current_superadmin from app.models.user import User router = APIRouter() DEFAULT_CONFIG_PATH = Path(__file__).parent.parent.parent.parent / "default_config.json" @router.get("/default-config") async def get_default_config( current_user: Annotated[User, Depends(get_current_superadmin)], ): """ Get default device configuration (superadmin only). Returns the default config that will be copied to newly registered devices. """ try: with open(DEFAULT_CONFIG_PATH, "r") as f: config = json.load(f) return config except FileNotFoundError: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Default config file not found", ) except json.JSONDecodeError: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Invalid JSON in default config file", ) @router.put("/default-config") async def update_default_config( config: dict, current_user: Annotated[User, Depends(get_current_superadmin)], ): """ Update default device configuration (superadmin only). Updates the default config file. Changes only affect newly registered devices. Existing devices keep their current configuration. """ try: # Write config to file with pretty formatting with open(DEFAULT_CONFIG_PATH, "w") as f: json.dump(config, f, indent=2) return {"success": True, "message": "Default config updated"} except Exception as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to save config: {str(e)}", )