"""
操作日志相关的Pydantic Schema
"""
from typing import Optional, List, Dict, Any
from datetime import datetime
from pydantic import BaseModel, Field
from enum import Enum


class OperationModuleEnum(str, Enum):
    """操作模块枚举"""
    AUTH = "auth"  # 认证模块
    ASSET = "asset"  # 资产模块
    DEVICE_TYPE = "device_type"  # 设备类型模块
    ORGANIZATION = "organization"  # 机构模块
    BRAND_SUPPLIER = "brand_supplier"  # 品牌供应商模块
    ALLOCATION = "allocation"  # 调拨模块
    MAINTENANCE = "maintenance"  # 维修模块
    SYSTEM_CONFIG = "system_config"  # 系统配置模块
    USER = "user"  # 用户模块
    STATISTICS = "statistics"  # 统计模块


class OperationTypeEnum(str, Enum):
    """操作类型枚举"""
    CREATE = "create"  # 创建
    UPDATE = "update"  # 更新
    DELETE = "delete"  # 删除
    QUERY = "query"  # 查询
    EXPORT = "export"  # 导出
    IMPORT = "import"  # 导入
    LOGIN = "login"  # 登录
    LOGOUT = "logout"  # 登出
    APPROVE = "approve"  # 审批
    REJECT = "reject"  # 拒绝
    ASSIGN = "assign"  # 分配
    TRANSFER = "transfer"  # 调拨
    SCRAP = "scrap"  # 报废


class OperationResultEnum(str, Enum):
    """操作结果枚举"""
    SUCCESS = "success"
    FAILED = "failed"


class OperationLogBase(BaseModel):
    """操作日志基础Schema"""
    operator_id: int = Field(..., description="操作人ID")
    operator_name: str = Field(..., min_length=1, max_length=100, description="操作人姓名")
    operator_ip: Optional[str] = Field(None, max_length=50, description="操作人IP")
    module: OperationModuleEnum = Field(..., description="模块名称")
    operation_type: OperationTypeEnum = Field(..., description="操作类型")
    method: str = Field(..., min_length=1, max_length=10, description="请求方法")
    url: str = Field(..., min_length=1, max_length=500, description="请求URL")
    params: Optional[str] = Field(None, description="请求参数")
    result: OperationResultEnum = Field(default=OperationResultEnum.SUCCESS, description="操作结果")
    error_msg: Optional[str] = Field(None, description="错误信息")
    duration: Optional[int] = Field(None, ge=0, description="执行时长(毫秒)")
    user_agent: Optional[str] = Field(None, max_length=500, description="用户代理")
    extra_data: Optional[Dict[str, Any]] = Field(None, description="额外数据")


class OperationLogCreate(OperationLogBase):
    """创建操作日志Schema"""
    pass


class OperationLogInDB(BaseModel):
    """数据库中的操作日志Schema"""
    id: int
    operator_id: int
    operator_name: str
    operator_ip: Optional[str]
    module: str
    operation_type: str
    method: str
    url: str
    params: Optional[str]
    result: str
    error_msg: Optional[str]
    duration: Optional[int]
    user_agent: Optional[str]
    extra_data: Optional[Dict[str, Any]]
    created_at: datetime

    class Config:
        from_attributes = True


class OperationLogResponse(OperationLogInDB):
    """操作日志响应Schema"""
    pass


class OperationLogQueryParams(BaseModel):
    """操作日志查询参数"""
    operator_id: Optional[int] = Field(None, description="操作人ID")
    operator_name: Optional[str] = Field(None, description="操作人姓名")
    module: Optional[OperationModuleEnum] = Field(None, description="模块名称")
    operation_type: Optional[OperationTypeEnum] = Field(None, description="操作类型")
    result: Optional[OperationResultEnum] = Field(None, description="操作结果")
    start_time: Optional[datetime] = Field(None, description="开始时间")
    end_time: Optional[datetime] = Field(None, description="结束时间")
    keyword: Optional[str] = Field(None, description="关键词")
    page: int = Field(default=1, ge=1, description="页码")
    page_size: int = Field(default=20, ge=1, le=100, description="每页数量")


class OperationLogStatistics(BaseModel):
    """操作日志统计Schema"""
    total_count: int = Field(..., description="总操作次数")
    success_count: int = Field(..., description="成功次数")
    failed_count: int = Field(..., description="失败次数")
    today_count: int = Field(..., description="今日操作次数")
    module_distribution: List[Dict[str, Any]] = Field(default_factory=list, description="模块分布")
    operation_distribution: List[Dict[str, Any]] = Field(default_factory=list, description="操作类型分布")


class OperationLogExport(BaseModel):
    """操作日志导出Schema"""
    start_time: Optional[datetime] = Field(None, description="开始时间")
    end_time: Optional[datetime] = Field(None, description="结束时间")
    operator_id: Optional[int] = Field(None, description="操作人ID")
    module: Optional[str] = Field(None, description="模块名称")
    operation_type: Optional[str] = Field(None, description="操作类型")
