"""
系统配置相关的Pydantic Schema
"""
from typing import Optional, List, Dict, Any
from datetime import datetime
from pydantic import BaseModel, Field
from enum import Enum


class ValueTypeEnum(str, Enum):
    """配置值类型枚举"""
    STRING = "string"
    NUMBER = "number"
    BOOLEAN = "boolean"
    JSON = "json"


class SystemConfigBase(BaseModel):
    """系统配置基础Schema"""
    config_key: str = Field(..., min_length=1, max_length=100, description="配置键")
    config_name: str = Field(..., min_length=1, max_length=200, description="配置名称")
    config_value: Optional[str] = Field(None, description="配置值")
    value_type: ValueTypeEnum = Field(default=ValueTypeEnum.STRING, description="值类型")
    category: str = Field(..., min_length=1, max_length=50, description="配置分类")
    description: Optional[str] = Field(None, description="配置描述")
    is_system: bool = Field(default=False, description="是否系统配置")
    is_encrypted: bool = Field(default=False, description="是否加密存储")
    validation_rule: Optional[str] = Field(None, description="验证规则")
    options: Optional[Dict[str, Any]] = Field(None, description="可选值配置")
    default_value: Optional[str] = Field(None, description="默认值")
    sort_order: int = Field(default=0, description="排序序号")
    is_active: bool = Field(default=True, description="是否启用")


class SystemConfigCreate(SystemConfigBase):
    """创建系统配置Schema"""
    pass


class SystemConfigUpdate(BaseModel):
    """更新系统配置Schema"""
    config_name: Optional[str] = Field(None, min_length=1, max_length=200)
    config_value: Optional[str] = None
    value_type: Optional[ValueTypeEnum] = None
    category: Optional[str] = Field(None, min_length=1, max_length=50)
    description: Optional[str] = None
    validation_rule: Optional[str] = None
    options: Optional[Dict[str, Any]] = None
    default_value: Optional[str] = None
    sort_order: Optional[int] = None
    is_active: Optional[bool] = None


class SystemConfigInDB(BaseModel):
    """数据库中的系统配置Schema"""
    id: int
    config_key: str
    config_name: str
    config_value: Optional[str]
    value_type: str
    category: str
    description: Optional[str]
    is_system: bool
    is_encrypted: bool
    validation_rule: Optional[str]
    options: Optional[Dict[str, Any]]
    default_value: Optional[str]
    sort_order: int
    is_active: bool
    created_at: datetime
    updated_at: datetime
    updated_by: Optional[int]

    class Config:
        from_attributes = True


class SystemConfigResponse(SystemConfigInDB):
    """系统配置响应Schema"""
    pass


class SystemConfigBatchUpdate(BaseModel):
    """批量更新配置Schema"""
    configs: Dict[str, Any] = Field(..., description="配置键值对")


class SystemConfigQueryParams(BaseModel):
    """系统配置查询参数"""
    keyword: Optional[str] = Field(None, description="搜索关键词")
    category: Optional[str] = Field(None, description="配置分类")
    is_active: Optional[bool] = Field(None, description="是否启用")
    is_system: Optional[bool] = Field(None, description="是否系统配置")
    page: int = Field(default=1, ge=1, description="页码")
    page_size: int = Field(default=20, ge=1, le=100, description="每页数量")


class ConfigCategoryResponse(BaseModel):
    """配置分类响应Schema"""
    category: str = Field(..., description="分类名称")
    count: int = Field(..., description="配置数量")
    description: Optional[str] = Field(None, description="分类描述")
