"""
API router for configuration management
"""
from fastapi import APIRouter, Depends, HTTPException

from app.core.dependencies import get_config_manager
from app.schemas.config import ConfigResponse, ConfigUpdateRequest, ConfigTestRequest, ConfigTestResponse
from app.services.config_manager import ConfigManager

router = APIRouter()

@router.get("/", response_model=ConfigResponse)
async def get_config(config_manager: ConfigManager = Depends(get_config_manager)):
    """Retrieve the current system configuration."""
    return await config_manager.get_config()

@router.put("/", response_model=ConfigResponse)
async def update_config(
    config_update: ConfigUpdateRequest,
    config_manager: ConfigManager = Depends(get_config_manager)
):
    """Update system configuration."""
    return await config_manager.update_config(config_update)

@router.post("/test", response_model=ConfigTestResponse)
async def test_config(
    test_request: ConfigTestRequest,
    config_manager: ConfigManager = Depends(get_config_manager)
):
    """Test a specific configuration (e.g., database connection)."""
    try:
        test_result = await config_manager.test_config(
            test_request.config_type,
            test_request.config_data
        )
        return test_result
    except Exception as e:
        return ConfigTestResponse(
            success=False,
            message=f"测试失败: {str(e)}"
        )
