# =============================================================================
# 企微IT智能服务台 — 员工 Pydantic Schema
# =============================================================================
# 说明：定义员工相关的请求/响应数据结构
# 包含：IT等级更新请求、员工响应 Schema
# =============================================================================

from datetime import datetime
from typing import Any, Dict, List, Optional

from pydantic import BaseModel, Field, field_validator


# --------------------------------------------------------------------------
# IT 等级合法值
# --------------------------------------------------------------------------
VALID_IT_LEVELS = {"bronze", "silver", "gold", "platinum", "diamond", "star", "king"}

# --------------------------------------------------------------------------
# 等级来源合法值
# --------------------------------------------------------------------------
VALID_LEVEL_SOURCES = {"system", "manual", "assessment"}


# --------------------------------------------------------------------------
# IT 等级更新请求 Schema
# --------------------------------------------------------------------------
class ItLevelUpdateRequest(BaseModel):
    """IT技能等级更新请求 Schema。

    坐席手动调整员工IT技能等级时使用。

    Attributes:
        it_level: 新的IT技能等级
        source: 等级来源（默认 manual）
    """

    it_level: str = Field(..., description="IT技能等级: bronze/silver/gold/platinum/diamond/star/king")
    source: str = Field(default="manual", description="等级来源: system/manual/assessment")

    @field_validator("it_level")
    @classmethod
    def validate_it_level(cls, v: str) -> str:
        """校验IT等级值是否合法。"""
        if v not in VALID_IT_LEVELS:
            raise ValueError(f"无效的IT等级: {v}，合法值为: {VALID_IT_LEVELS}")
        return v

    @field_validator("source")
    @classmethod
    def validate_source(cls, v: str) -> str:
        """校验等级来源值是否合法。"""
        if v not in VALID_LEVEL_SOURCES:
            raise ValueError(f"无效的等级来源: {v}，合法值为: {VALID_LEVEL_SOURCES}")
        return v


# --------------------------------------------------------------------------
# 员工响应 Schema（返回给前端的数据结构）
# --------------------------------------------------------------------------
class EmployeeResponse(BaseModel):
    """员工响应 Schema。

    返回给前端的员工数据结构。
    使用 from_attributes=True 支持从 SQLAlchemy 模型直接转换。

    Attributes:
        id: 员工记录唯一标识
        corp_id: 企业微信企业ID
        employee_id: 企微员工UserID
        name: 员工姓名
        department: 部门
        position: 岗位
        mobile: 手机号
        email: 邮箱
        avatar: 头像URL
        status: 激活状态
        it_level: IT技能等级
        it_level_source: 等级来源
        notes: 坐席备注
        last_login_at: 最后登录时间
        created_at: 创建时间
        updated_at: 更新时间
    """

    id: str
    corp_id: str
    employee_id: str
    name: str
    department: str
    position: str
    mobile: str
    email: str
    avatar: str
    status: int
    it_level: str = "silver"
    it_level_source: str = "system"
    notes: Dict[str, Any] = Field(default_factory=dict, description="坐席备注")
    last_login_at: Optional[datetime] = None
    created_at: datetime
    updated_at: datetime

    model_config = {"from_attributes": True}


# --------------------------------------------------------------------------
# 员工列表响应 Schema
# --------------------------------------------------------------------------
class EmployeeListResponse(BaseModel):
    """员工列表响应 Schema。

    Attributes:
        items: 员工列表
        total: 总数
    """

    items: List[EmployeeResponse]
    total: int
