# =============================================================================
# IT智能服务台 — 错误码定义
# =============================================================================
# 说明：统一管理系统错误码，便于前端解析和国际化
# 格式：E{模块}{序号}
# =============================================================================

from enum import Enum


class ErrorCode(str, Enum):
    """系统错误码枚举"""

    # --------------------------------------------------------------------------
    # 通用错误 (0xxx)
    # --------------------------------------------------------------------------
    SUCCESS = "E0000"  # 成功
    UNKNOWN_ERROR = "E0001"  # 未知错误
    INVALID_PARAMETER = "E0002"  # 参数错误
    MISSING_PARAMETER = "E0003"  # 缺少参数
    NOT_FOUND = "E0004"  # 资源不存在
    UNAUTHORIZED = "E0005"  # 未授权
    FORBIDDEN = "E0006"  # 禁止访问
    INTERNAL_ERROR = "E0007"  # 内部错误
    SERVICE_UNAVAILABLE = "E0008"  # 服务不可用
    TIMEOUT = "E0009"  # 请求超时

    # --------------------------------------------------------------------------
    # 认证相关 (1xxx)
    # --------------------------------------------------------------------------
    AUTH_FAILED = "E1001"  # 认证失败
    AUTH_TOKEN_EXPIRED = "E1002"  # Token过期
    AUTH_TOKEN_INVALID = "E1003"  # Token无效
    AUTH_PASSWORD_REQUIRED = "E1012"  # 登录:首次登录请先设置密码
    AUTH_PASSWORD_WRONG = "E1011"  # 登录:本地密码错误
    AUTH_OLD_PASSWORD_REQUIRED = "E1015"  # 改密:请输入旧密码(2026-06-15 WB反馈 1012 上下文冲突后拆分)
    AUTH_OLD_PASSWORD_WRONG = "E1016"  # 改密:旧密码错误(2026-06-15 拆分)

    # --------------------------------------------------------------------------
    # 企微API错误 (2xxx)
    # --------------------------------------------------------------------------
    WECOM_API_ERROR = "E2001"  # 企微API调用失败
    WECOM_API_TIMEOUT = "E2002"  # 企微API超时
    WECOM_TOKEN_INVALID = "E2003"  # 企微token无效
    WECOM_USER_NOT_FOUND = "E2004"  # 企微用户不存在

    # --------------------------------------------------------------------------
    # 会话/消息错误 (3xxx)
    # --------------------------------------------------------------------------
    CONVERSATION_NOT_FOUND = "E3001"  # 会话不存在
    MESSAGE_NOT_FOUND = "E3002"  # 消息不存在
    MESSAGE_TOO_LONG = "E3003"  # 消息过长
    CONVERSATION_CLOSED = "E3004"  # 会话已关闭

    # --------------------------------------------------------------------------
    # 坐席错误 (4xxx)
    # --------------------------------------------------------------------------
    AGENT_NOT_FOUND = "E4001"  # 坐席不存在
    AGENT_OFFLINE = "E4002"  # 坐席不在线
    AGENT_BUSY = "E4003"  # 坐席忙碌
    AGENT_MAX_LOAD = "E4004"  # 坐席已达最大接待量

    # --------------------------------------------------------------------------
    # 审批错误 (5xxx)
    # --------------------------------------------------------------------------
    APPROVAL_TEMPLATE_NOT_FOUND = "E5001"  # 审批模板不存在
    APPROVAL_FAILED = "E5002"  # 审批提交失败

    # --------------------------------------------------------------------------
    # 文件上传错误 (6xxx)
    # --------------------------------------------------------------------------
    FILE_TOO_LARGE = "E6001"  # 文件过大
    FILE_TYPE_NOT_ALLOWED = "E6002"  # 文件类型不允许
    FILE_UPLOAD_FAILED = "E6003"  # 文件上传失败


# 错误码到 HTTP 状态码的映射
ERROR_CODE_TO_STATUS = {
    ErrorCode.SUCCESS: 200,
    ErrorCode.INVALID_PARAMETER: 400,
    ErrorCode.MISSING_PARAMETER: 400,
    ErrorCode.NOT_FOUND: 404,
    ErrorCode.UNAUTHORIZED: 401,
    ErrorCode.FORBIDDEN: 403,
    ErrorCode.INTERNAL_ERROR: 500,
    ErrorCode.SERVICE_UNAVAILABLE: 503,
    # 认证
    ErrorCode.AUTH_FAILED: 401,
    ErrorCode.AUTH_TOKEN_EXPIRED: 401,
    ErrorCode.AUTH_TOKEN_INVALID: 401,
    ErrorCode.AUTH_PASSWORD_REQUIRED: 401,
    ErrorCode.AUTH_PASSWORD_WRONG: 401,
    ErrorCode.AUTH_OLD_PASSWORD_REQUIRED: 400,
    ErrorCode.AUTH_OLD_PASSWORD_WRONG: 400,
    # 企微
    ErrorCode.WECOM_API_ERROR: 502,
    ErrorCode.WECOM_API_TIMEOUT: 504,
    ErrorCode.WECOM_TOKEN_INVALID: 401,
    ErrorCode.WECOM_USER_NOT_FOUND: 404,
    # 会话
    ErrorCode.CONVERSATION_NOT_FOUND: 404,
    ErrorCode.MESSAGE_NOT_FOUND: 404,
    ErrorCode.MESSAGE_TOO_LONG: 400,
    ErrorCode.CONVERSATION_CLOSED: 400,
    # 坐席
    ErrorCode.AGENT_NOT_FOUND: 404,
    ErrorCode.AGENT_OFFLINE: 400,
    ErrorCode.AGENT_BUSY: 400,
    ErrorCode.AGENT_MAX_LOAD: 400,
    # 审批
    ErrorCode.APPROVAL_TEMPLATE_NOT_FOUND: 404,
    ErrorCode.APPROVAL_FAILED: 502,
    # 文件
    ErrorCode.FILE_TOO_LARGE: 413,
    ErrorCode.FILE_TYPE_NOT_ALLOWED: 400,
    ErrorCode.FILE_UPLOAD_FAILED: 500,
}


def get_error_message(code: ErrorCode) -> str:
    """获取错误码对应的默认消息"""
    messages = {
        ErrorCode.SUCCESS: "操作成功",
        ErrorCode.UNKNOWN_ERROR: "未知错误，请稍后重试",
        ErrorCode.INVALID_PARAMETER: "参数错误",
        ErrorCode.MISSING_PARAMETER: "缺少必要参数",
        ErrorCode.NOT_FOUND: "资源不存在",
        ErrorCode.UNAUTHORIZED: "未授权，请先登录",
        ErrorCode.FORBIDDEN: "禁止访问",
        ErrorCode.INTERNAL_ERROR: "服务器内部错误",
        ErrorCode.SERVICE_UNAVAILABLE: "服务暂时不可用",
        ErrorCode.TIMEOUT: "请求超时",
        ErrorCode.AUTH_FAILED: "认证失败",
        ErrorCode.AUTH_TOKEN_EXPIRED: "登录已过期，请重新登录",
        ErrorCode.AUTH_TOKEN_INVALID: "无效的登录凭证",
        ErrorCode.AUTH_PASSWORD_REQUIRED: "首次登录请先设置密码",
        ErrorCode.AUTH_PASSWORD_WRONG: "密码错误",
        ErrorCode.AUTH_OLD_PASSWORD_REQUIRED: "请输入旧密码",
        ErrorCode.AUTH_OLD_PASSWORD_WRONG: "旧密码错误",
        ErrorCode.WECOM_API_ERROR: "企业微信服务异常",
        ErrorCode.WECOM_API_TIMEOUT: "企业微信服务响应超时",
        ErrorCode.WECOM_TOKEN_INVALID: "企业微信凭证无效",
        ErrorCode.WECOM_USER_NOT_FOUND: "企业微信用户不存在",
        ErrorCode.CONVERSATION_NOT_FOUND: "会话不存在",
        ErrorCode.MESSAGE_NOT_FOUND: "消息不存在",
        ErrorCode.MESSAGE_TOO_LONG: "消息内容过长",
        ErrorCode.CONVERSATION_CLOSED: "会话已结束",
        ErrorCode.AGENT_NOT_FOUND: "坐席不存在",
        ErrorCode.AGENT_OFFLINE: "坐席不在线",
        ErrorCode.AGENT_BUSY: "坐席忙碌中",
        ErrorCode.AGENT_MAX_LOAD: "坐席已达到最大接待量",
        ErrorCode.APPROVAL_TEMPLATE_NOT_FOUND: "审批模板不存在",
        ErrorCode.APPROVAL_FAILED: "审批提交失败",
        ErrorCode.FILE_TOO_LARGE: "文件过大",
        ErrorCode.FILE_TYPE_NOT_ALLOWED: "不支持的文件类型",
        ErrorCode.FILE_UPLOAD_FAILED: "文件上传失败",
    }
    return messages.get(code, "未知错误")
