# =============================================================================
# 企微IT智能服务台 — 员工 API
# =============================================================================
# 说明：提供员工相关的管理接口
# 接口列表：
#   PUT /api/employees/{employee_id}/it-level — 更新员工IT技能等级
# =============================================================================

from typing import Optional

from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, Field, field_validator

from app.utils.response import success_response

from app.schemas.employee import VALID_IT_LEVELS, VALID_LEVEL_SOURCES

# 创建路由器
router = APIRouter(prefix="/employees", tags=["员工管理"])


# --------------------------------------------------------------------------
# 请求 Schema
# --------------------------------------------------------------------------

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

    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


class ItLevelUpdateResponse(BaseModel):
    """IT技能等级更新响应 Schema。"""

    employee_id: str
    it_level: str
    it_level_source: str
    message: str


# --------------------------------------------------------------------------
# Mock 员工数据存储（IT 等级映射）
# --------------------------------------------------------------------------

# 简单的内存存储，key 为 employee_id，value 为 it_level
MOCK_EMPLOYEE_IT_LEVELS: dict = {
    "emp-001": "silver",
    "emp-002": "gold",
    "emp-003": "bronze",
    "emp-004": "platinum",
    "emp-005": "diamond",
    "emp-006": "silver",
    "emp-007": "star",
    "emp-008": "king",
}


# --------------------------------------------------------------------------
# API 接口
# --------------------------------------------------------------------------

@router.put("/{employee_id}/it-level")
async def update_employee_it_level(
    employee_id: str,
    request: ItLevelUpdateRequest,
):
    """更新员工IT技能等级。

    坐席可以手动调整员工的IT技能等级，等级来源标记为 manual。
    更新后等级立即生效，并记录来源以便追溯。

    Args:
        employee_id: 员工ID
        request: 等级更新请求

    Returns:
        更新结果
    """
    # 更新内存中的等级
    old_level = MOCK_EMPLOYEE_IT_LEVELS.get(employee_id, "silver")
    MOCK_EMPLOYEE_IT_LEVELS[employee_id] = request.it_level

    # 构造等级名称映射
    level_names = {
        "bronze": "青铜",
        "silver": "白银",
        "gold": "黄金",
        "platinum": "铂金",
        "diamond": "钻石",
        "star": "星耀",
        "king": "王者",
    }

    return success_response(data=ItLevelUpdateResponse(
        employee_id=employee_id,
        it_level=request.it_level,
        it_level_source=request.source,
        message=f"IT等级已从 {level_names.get(old_level, old_level)} 调整为 {level_names.get(request.it_level, request.it_level)}",
    ).model_dump())
