/**
 * Error codes for the application
 */

export const ERROR_CODES = {
  // Authentication errors (1xxx)
  AUTH_INVALID_TOKEN: 'AUTH_1001',
  AUTH_TOKEN_EXPIRED: 'AUTH_1002',
  AUTH_UNAUTHORIZED: 'AUTH_1003',
  AUTH_FORBIDDEN: 'AUTH_1004',
  AUTH_INVALID_CREDENTIALS: 'AUTH_1005',
  AUTH_PHONE_NOT_VERIFIED: 'AUTH_1006',
  AUTH_VERIFICATION_CODE_INVALID: 'AUTH_1007',
  AUTH_VERIFICATION_CODE_EXPIRED: 'AUTH_1008',

  // User errors (2xxx)
  USER_NOT_FOUND: 'USER_2001',
  USER_ALREADY_EXISTS: 'USER_2002',
  USER_INVALID_FINGERPRINT: 'USER_2003',
  USER_PHONE_ALREADY_REGISTERED: 'USER_2004',

  // Conversation errors (3xxx)
  CONVERSATION_NOT_FOUND: 'CONV_3001',
  CONVERSATION_ALREADY_ENDED: 'CONV_3002',
  CONVERSATION_ACCESS_DENIED: 'CONV_3003',
  MESSAGE_NOT_FOUND: 'CONV_3004',
  MESSAGE_SEND_FAILED: 'CONV_3005',

  // Payment errors (4xxx)
  PAYMENT_ORDER_NOT_FOUND: 'PAY_4001',
  PAYMENT_ALREADY_PAID: 'PAY_4002',
  PAYMENT_EXPIRED: 'PAY_4003',
  PAYMENT_FAILED: 'PAY_4004',
  PAYMENT_INVALID_CALLBACK: 'PAY_4005',
  PAYMENT_AMOUNT_MISMATCH: 'PAY_4006',
  PAYMENT_METHOD_NOT_SUPPORTED: 'PAY_4007',

  // Knowledge errors (5xxx)
  KNOWLEDGE_DOCUMENT_NOT_FOUND: 'KNOW_5001',
  KNOWLEDGE_SEARCH_FAILED: 'KNOW_5002',
  KNOWLEDGE_EMBEDDING_FAILED: 'KNOW_5003',
  KNOWLEDGE_GRAPH_ERROR: 'KNOW_5004',

  // Assessment errors (6xxx)
  ASSESSMENT_CATEGORY_INVALID: 'ASSESS_6001',
  ASSESSMENT_DATA_INCOMPLETE: 'ASSESS_6002',
  ASSESSMENT_FAILED: 'ASSESS_6003',

  // Admin errors (7xxx)
  ADMIN_NOT_FOUND: 'ADMIN_7001',
  ADMIN_PERMISSION_DENIED: 'ADMIN_7002',
  ADMIN_INVALID_ROLE: 'ADMIN_7003',

  // Evolution errors (8xxx)
  EVOLUTION_FAILED: 'EVO_8001',
  EVOLUTION_ALREADY_APPLIED: 'EVO_8002',
  EVOLUTION_ROLLBACK_FAILED: 'EVO_8003',

  // Rate limiting errors (9xxx)
  RATE_LIMIT_EXCEEDED: 'RATE_9001',

  // Tenant errors (11xxx)
  TENANT_NOT_FOUND: 'TENANT_11001',
  TENANT_SUSPENDED: 'TENANT_11002',
  TENANT_ARCHIVED: 'TENANT_11003',
  TENANT_CONTEXT_REQUIRED: 'TENANT_11004',
  TENANT_FEATURE_DISABLED: 'TENANT_11005',
  TENANT_PLAN_UPGRADE_REQUIRED: 'TENANT_11006',
  TENANT_QUOTA_EXCEEDED: 'TENANT_11007',
  TENANT_ALREADY_EXISTS: 'TENANT_11008',

  // System errors (10xxx)
  INTERNAL_ERROR: 'SYS_10001',
  SERVICE_UNAVAILABLE: 'SYS_10002',
  DATABASE_ERROR: 'SYS_10003',
  EXTERNAL_SERVICE_ERROR: 'SYS_10004',
  CLAUDE_API_ERROR: 'SYS_10005',
} as const;

export type ErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES];

export const ERROR_MESSAGES: Record<ErrorCode, string> = {
  [ERROR_CODES.AUTH_INVALID_TOKEN]: '无效的认证令牌',
  [ERROR_CODES.AUTH_TOKEN_EXPIRED]: '认证令牌已过期',
  [ERROR_CODES.AUTH_UNAUTHORIZED]: '未授权访问',
  [ERROR_CODES.AUTH_FORBIDDEN]: '禁止访问',
  [ERROR_CODES.AUTH_INVALID_CREDENTIALS]: '用户名或密码错误',
  [ERROR_CODES.AUTH_PHONE_NOT_VERIFIED]: '手机号未验证',
  [ERROR_CODES.AUTH_VERIFICATION_CODE_INVALID]: '验证码错误',
  [ERROR_CODES.AUTH_VERIFICATION_CODE_EXPIRED]: '验证码已过期',

  [ERROR_CODES.USER_NOT_FOUND]: '用户不存在',
  [ERROR_CODES.USER_ALREADY_EXISTS]: '用户已存在',
  [ERROR_CODES.USER_INVALID_FINGERPRINT]: '无效的设备指纹',
  [ERROR_CODES.USER_PHONE_ALREADY_REGISTERED]: '该手机号已注册',

  [ERROR_CODES.CONVERSATION_NOT_FOUND]: '对话不存在',
  [ERROR_CODES.CONVERSATION_ALREADY_ENDED]: '对话已结束',
  [ERROR_CODES.CONVERSATION_ACCESS_DENIED]: '无权访问该对话',
  [ERROR_CODES.MESSAGE_NOT_FOUND]: '消息不存在',
  [ERROR_CODES.MESSAGE_SEND_FAILED]: '消息发送失败',

  [ERROR_CODES.PAYMENT_ORDER_NOT_FOUND]: '订单不存在',
  [ERROR_CODES.PAYMENT_ALREADY_PAID]: '订单已支付',
  [ERROR_CODES.PAYMENT_EXPIRED]: '支付已过期',
  [ERROR_CODES.PAYMENT_FAILED]: '支付失败',
  [ERROR_CODES.PAYMENT_INVALID_CALLBACK]: '无效的支付回调',
  [ERROR_CODES.PAYMENT_AMOUNT_MISMATCH]: '支付金额不匹配',
  [ERROR_CODES.PAYMENT_METHOD_NOT_SUPPORTED]: '不支持的支付方式',

  [ERROR_CODES.KNOWLEDGE_DOCUMENT_NOT_FOUND]: '文档不存在',
  [ERROR_CODES.KNOWLEDGE_SEARCH_FAILED]: '知识库搜索失败',
  [ERROR_CODES.KNOWLEDGE_EMBEDDING_FAILED]: '向量化处理失败',
  [ERROR_CODES.KNOWLEDGE_GRAPH_ERROR]: '知识图谱操作失败',

  [ERROR_CODES.ASSESSMENT_CATEGORY_INVALID]: '无效的移民类别',
  [ERROR_CODES.ASSESSMENT_DATA_INCOMPLETE]: '评估资料不完整',
  [ERROR_CODES.ASSESSMENT_FAILED]: '评估处理失败',

  [ERROR_CODES.ADMIN_NOT_FOUND]: '管理员不存在',
  [ERROR_CODES.ADMIN_PERMISSION_DENIED]: '权限不足',
  [ERROR_CODES.ADMIN_INVALID_ROLE]: '无效的角色',

  [ERROR_CODES.EVOLUTION_FAILED]: '系统进化失败',
  [ERROR_CODES.EVOLUTION_ALREADY_APPLIED]: '进化已应用',
  [ERROR_CODES.EVOLUTION_ROLLBACK_FAILED]: '进化回滚失败',

  [ERROR_CODES.RATE_LIMIT_EXCEEDED]: '请求过于频繁，请稍后再试',

  [ERROR_CODES.TENANT_NOT_FOUND]: '租户不存在',
  [ERROR_CODES.TENANT_SUSPENDED]: '租户已暂停服务',
  [ERROR_CODES.TENANT_ARCHIVED]: '租户已归档',
  [ERROR_CODES.TENANT_CONTEXT_REQUIRED]: '需要租户上下文',
  [ERROR_CODES.TENANT_FEATURE_DISABLED]: '该功能未启用',
  [ERROR_CODES.TENANT_PLAN_UPGRADE_REQUIRED]: '需要升级套餐',
  [ERROR_CODES.TENANT_QUOTA_EXCEEDED]: '租户配额已用尽',
  [ERROR_CODES.TENANT_ALREADY_EXISTS]: '租户已存在',

  [ERROR_CODES.INTERNAL_ERROR]: '系统内部错误',
  [ERROR_CODES.SERVICE_UNAVAILABLE]: '服务暂时不可用',
  [ERROR_CODES.DATABASE_ERROR]: '数据库错误',
  [ERROR_CODES.EXTERNAL_SERVICE_ERROR]: '外部服务错误',
  [ERROR_CODES.CLAUDE_API_ERROR]: 'Claude API 调用失败',
};
