"""
系统配置管理API路由
"""
from typing import Optional, List, Dict, Any
from fastapi import APIRouter, Depends, HTTPException, status, Query
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.deps import get_db, get_current_user
from app.schemas.system_config import (
    SystemConfigCreate,
    SystemConfigUpdate,
    SystemConfigResponse,
    SystemConfigBatchUpdate,
    SystemConfigQueryParams,
    ConfigCategoryResponse
)
from app.services.system_config_service import system_config_service

router = APIRouter()


@router.get("/", response_model=Dict[str, Any])
async def get_configs(
    skip: int = Query(0, ge=0, description="跳过条数"),
    limit: int = Query(20, ge=1, le=100, description="返回条数"),
    keyword: Optional[str] = Query(None, description="搜索关键词"),
    category: Optional[str] = Query(None, description="配置分类"),
    is_active: Optional[bool] = Query(None, description="是否启用"),
    is_system: Optional[bool] = Query(None, description="是否系统配置"),
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    获取系统配置列表

    - **skip**: 跳过条数
    - **limit**: 返回条数（最大100）
    - **keyword**: 搜索关键词（配置键/配置名称/描述）
    - **category**: 配置分类筛选
    - **is_active**: 是否启用筛选
    - **is_system**: 是否系统配置筛选
    """
    return await system_config_service.get_configs(
        db,
        skip=skip,
        limit=limit,
        keyword=keyword,
        category=category,
        is_active=is_active,
        is_system=is_system
    )


@router.get("/categories", response_model=List[Dict[str, Any]])
async def get_config_categories(
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    获取所有配置分类

    返回配置分类及每个分类的配置数量
    """
    return await system_config_service.get_categories(db)


@router.get("/category/{category}", response_model=List[Dict[str, Any]])
async def get_configs_by_category(
    category: str,
    is_active: bool = Query(True, description="是否启用"),
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    根据分类获取配置

    - **category**: 配置分类
    - **is_active**: 是否启用
    """
    return await system_config_service.get_configs_by_category(
        db,
        category=category,
        is_active=is_active
    )


@router.get("/key/{config_key}", response_model=Any)
async def get_config_by_key(
    config_key: str,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    根据配置键获取配置值

    - **config_key**: 配置键

    返回配置的实际值（已根据类型转换）
    """
    value = await system_config_service.get_config_by_key(db, config_key)
    if value is None:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"配置键 {config_key} 不存在或未启用"
        )
    return {"config_key": config_key, "value": value}


@router.get("/{config_id}", response_model=Dict[str, Any])
async def get_config(
    config_id: int,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    获取配置详情

    - **config_id**: 配置ID
    """
    config = await system_config_service.get_config(db, config_id)
    if not config:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="配置不存在"
        )
    return config


@router.post("/", response_model=Dict[str, Any], status_code=status.HTTP_201_CREATED)
async def create_config(
    obj_in: SystemConfigCreate,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    创建系统配置

    - **config_key**: 配置键（唯一）
    - **config_name**: 配置名称
    - **config_value**: 配置值
    - **value_type**: 值类型（string/number/boolean/json）
    - **category**: 配置分类
    - **description**: 配置描述
    - **is_system**: 是否系统配置（系统配置不允许删除和修改部分字段）
    - **is_encrypted**: 是否加密存储
    - **options**: 可选值配置
    - **default_value**: 默认值
    - **sort_order**: 排序序号
    - **is_active**: 是否启用
    """
    try:
        return await system_config_service.create_config(
            db,
            obj_in=obj_in,
            creator_id=current_user.id
        )
    except ValueError as e:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=str(e)
        )


@router.put("/{config_id}", response_model=Dict[str, Any])
async def update_config(
    config_id: int,
    obj_in: SystemConfigUpdate,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    更新系统配置

    - **config_id**: 配置ID
    - **config_name**: 配置名称
    - **config_value**: 配置值
    - **description**: 配置描述
    - **options**: 可选值配置
    - **default_value**: 默认值
    - **sort_order**: 排序序号
    - **is_active**: 是否启用
    """
    try:
        return await system_config_service.update_config(
            db,
            config_id=config_id,
            obj_in=obj_in,
            updater_id=current_user.id
        )
    except ValueError as e:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=str(e)
        )


@router.post("/batch", response_model=Dict[str, Any])
async def batch_update_configs(
    batch_update: SystemConfigBatchUpdate,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    批量更新配置

    - **configs**: 配置键值对字典

    示例：
    ```json
    {
        "configs": {
            "system.title": "资产管理系统",
            "system.max_upload_size": 10485760
        }
    }
    ```
    """
    return await system_config_service.batch_update_configs(
        db,
        configs=batch_update.configs,
        updater_id=current_user.id
    )


@router.delete("/{config_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_config(
    config_id: int,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    删除系统配置

    - **config_id**: 配置ID

    注意：系统配置不允许删除
    """
    try:
        await system_config_service.delete_config(db, config_id)
    except ValueError as e:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=str(e)
        )
    return None
