from enum import IntEnum


class BizCode(IntEnum):
    # 通用（1xxx）
    OK = 0
    BAD_REQUEST = 1000
    VALIDATION_FAILED = 1001
    MISSING_PARAMETER = 1002
    INVALID_PARAMETER = 1003
    # 认证/鉴权（2xxx/3xxx）
    UNAUTHORIZED = 2001
    TOKEN_INVALID = 2002
    TOKEN_EXPIRED = 2003
    TOKEN_BLACKLISTED = 2004
    PASSWORD_ERROR = 2005
    LOGIN_FAILED = 2006
    FORBIDDEN = 3001
    TENANT_NOT_FOUND = 3002
    WORKSPACE_NO_ACCESS = 3003
    WORKSPACE_INVITE_NOT_FOUND = 3004
    # API Key 管理（3xxx）
    API_KEY_NOT_FOUND = 3007
    API_KEY_DUPLICATE_NAME = 3008
    API_KEY_INVALID = 3009
    API_KEY_EXPIRED = 3010
    API_KEY_INACTIVE = 3011
    API_KEY_INVALID_SCOPE = 3012
    API_KEY_INVALID_RESOURCE = 3013
    API_KEY_QPS_LIMIT_EXCEEDED = 3014
    API_KEY_DAILY_LIMIT_EXCEEDED = 3015
    API_KEY_QUOTA_EXCEEDED = 3016
    # 资源（4xxx）
    NOT_FOUND = 4000
    USER_NOT_FOUND = 4001
    WORKSPACE_NOT_FOUND = 4002
    MODEL_NOT_FOUND = 4003
    KNOWLEDGE_NOT_FOUND = 4004
    DOCUMENT_NOT_FOUND = 4005
    FILE_NOT_FOUND = 4006
    APP_NOT_FOUND = 4007
    RELEASE_NOT_FOUND = 4008

    # 冲突/状态（5xxx）
    DUPLICATE_NAME = 5001
    RESOURCE_ALREADY_EXISTS = 5002
    VERSION_ALREADY_EXISTS = 5003
    STATE_CONFLICT = 5004
    RESOURCE_IN_USE = 5005

    # 应用发布（6xxx）
    PUBLISH_FAILED = 6001
    NO_DRAFT_TO_PUBLISH = 6002
    ROLLBACK_TARGET_NOT_FOUND = 6003
    APP_TYPE_NOT_SUPPORTED = 6004
    AGENT_CONFIG_MISSING = 6005
    SHARE_DISABLED = 6006
    INVALID_PASSWORD = 6007
    PASSWORD_REQUIRED = 6008
    EMBED_NOT_ALLOWED = 6009
    PERMISSION_DENIED = 6010
    INVALID_CONVERSATION = 6011
    CONFIG_MISSING = 6012

    # 模型（7xxx）
    MODEL_CONFIG_INVALID = 7001
    API_KEY_MISSING = 7002
    PROVIDER_NOT_SUPPORTED = 7003
    LLM_ERROR = 7004
    EMBEDDING_ERROR = 7005

    # 文件/解析（8xxx）
    FILE_READ_ERROR = 8001
    PARSER_NOT_SUPPORTED = 8002
    CHUNKING_FAILED = 8003

    # RAG/知识（9xxx）
    INDEX_BUILD_FAILED = 9001
    EMBEDDING_FAILED = 9002
    SEARCH_FAILED = 9003

    # Memory API（95xx）
    MEMORY_WRITE_FAILED = 9501
    MEMORY_READ_FAILED = 9502
    MEMORY_CONFIG_NOT_FOUND = 9503
    
    # Implicit Memory API（96xx）
    INVALID_USER_ID = 9601
    INSUFFICIENT_DATA = 9602
    INVALID_FILTER_PARAMS = 9603
    ANALYSIS_FAILED = 9604
    PROFILE_STORAGE_ERROR = 9605

    # 系统（100xx）
    INTERNAL_ERROR = 10001
    DB_ERROR = 10002
    SERVICE_UNAVAILABLE = 10003
    RATE_LIMITED = 10004


# 建议的HTTP状态映射（如需在异常处理器中使用）
HTTP_MAPPING = {
    BizCode.OK: 200,
    BizCode.LOGIN_FAILED: 200,
    BizCode.BAD_REQUEST: 400,
    BizCode.VALIDATION_FAILED: 400,
    BizCode.MISSING_PARAMETER: 400,
    BizCode.INVALID_PARAMETER: 400,
    BizCode.UNAUTHORIZED: 401,
    BizCode.TOKEN_INVALID: 401,
    BizCode.TOKEN_EXPIRED: 401,
    BizCode.TOKEN_BLACKLISTED: 401,
    BizCode.FORBIDDEN: 403,
    BizCode.TENANT_NOT_FOUND: 400,
    BizCode.WORKSPACE_NO_ACCESS: 403,
    BizCode.NOT_FOUND: 400,
    BizCode.USER_NOT_FOUND: 200,
    BizCode.WORKSPACE_NOT_FOUND: 400,
    BizCode.MODEL_NOT_FOUND: 400,
    BizCode.KNOWLEDGE_NOT_FOUND: 400,
    BizCode.DOCUMENT_NOT_FOUND: 400,
    BizCode.FILE_NOT_FOUND: 400,
    BizCode.APP_NOT_FOUND: 400,
    BizCode.RELEASE_NOT_FOUND: 400,
    BizCode.DUPLICATE_NAME: 409,
    BizCode.RESOURCE_ALREADY_EXISTS: 409,
    BizCode.VERSION_ALREADY_EXISTS: 409,
    BizCode.STATE_CONFLICT: 409,
    BizCode.RESOURCE_IN_USE: 409,
    BizCode.PUBLISH_FAILED: 500,
    BizCode.NO_DRAFT_TO_PUBLISH: 400,
    BizCode.ROLLBACK_TARGET_NOT_FOUND: 400,
    BizCode.APP_TYPE_NOT_SUPPORTED: 400,
    BizCode.AGENT_CONFIG_MISSING: 400,
    BizCode.SHARE_DISABLED: 403,
    BizCode.INVALID_PASSWORD: 401,
    BizCode.PASSWORD_REQUIRED: 401,
    BizCode.EMBED_NOT_ALLOWED: 403,
    BizCode.PERMISSION_DENIED: 403,
    BizCode.INVALID_CONVERSATION: 400,
    
    # API Key 错误码映射
    BizCode.API_KEY_NOT_FOUND: 400,
    BizCode.API_KEY_DUPLICATE_NAME: 400,
    BizCode.API_KEY_INVALID: 401,
    BizCode.API_KEY_EXPIRED: 401,
    BizCode.API_KEY_INACTIVE: 401,
    BizCode.API_KEY_INVALID_SCOPE: 403,
    BizCode.API_KEY_INVALID_RESOURCE: 403,
    BizCode.API_KEY_QPS_LIMIT_EXCEEDED: 429,
    BizCode.API_KEY_DAILY_LIMIT_EXCEEDED: 429,
    BizCode.API_KEY_QUOTA_EXCEEDED: 429,
    
    BizCode.MODEL_CONFIG_INVALID: 400,
    BizCode.API_KEY_MISSING: 400,
    BizCode.PROVIDER_NOT_SUPPORTED: 400,
    BizCode.LLM_ERROR: 500,
    BizCode.EMBEDDING_ERROR: 500,
    BizCode.FILE_READ_ERROR: 500,
    BizCode.PARSER_NOT_SUPPORTED: 400,
    BizCode.CHUNKING_FAILED: 500,
    BizCode.INDEX_BUILD_FAILED: 500,
    BizCode.EMBEDDING_FAILED: 500,
    BizCode.SEARCH_FAILED: 500,
    
    # Memory API 错误码映射
    BizCode.MEMORY_WRITE_FAILED: 500,
    BizCode.MEMORY_READ_FAILED: 500,
    BizCode.MEMORY_CONFIG_NOT_FOUND: 400,
    
    # Implicit Memory API 错误码映射
    BizCode.INVALID_USER_ID: 400,
    BizCode.INSUFFICIENT_DATA: 400,
    BizCode.INVALID_FILTER_PARAMS: 400,
    BizCode.ANALYSIS_FAILED: 500,
    BizCode.PROFILE_STORAGE_ERROR: 500,
    
    BizCode.INTERNAL_ERROR: 500,
    BizCode.DB_ERROR: 500,
    BizCode.SERVICE_UNAVAILABLE: 503,
    BizCode.RATE_LIMITED: 429,
}
