/**
 * Application error codes with unique identifiers and descriptions
 */
export interface AppErrorCodeDefinition {
  code: string
  description: string
}

export const AppErrorCode = {
  // Generic Errors
  GENERIC_NOT_FOUND: {
    code: "CF-GEN-404",
    description: "The requested resource was not found",
  },
  GENERIC_BAD_REQUEST: {
    code: "CF-GEN-400",
    description: "The request was invalid or cannot be processed",
  },
  UNAUTHORIZED_ACCESS: {
    code: "CF-GEN-403",
    description: "Unauthorized access",
  },
  GENERIC_INTERNAL_SERVER_ERROR: {
    code: "CF-GEN-500",
    description: "An unexpected error occurred",
  },

  // Validation Errors
  INVALID_PARAMETER: {
    code: "CF-VAL-4001",
    description: "Invalid parameter provided",
  },
  VALIDATION_FAILURE: {
    code: "CF-VAL-4002",
    description: "Validation failure",
  },
  PAYLOAD_TOO_LARGE: {
    code: "CF-VAL-4003",
    description: "Payload too large",
  },
  FILE_SIZE_EXCEEDED: {
    code: "CF-VAL-4004",
    description: "File size exceeded",
  },
  INVALID_FILE_TYPE: {
    code: "CF-VAL-4005",
    description: "Invalid file type",
  },
  MISSING_REQUIRED_FIELDS: {
    code: "CF-VAL-4006",
    description: "Missing required fields",
  },
  CONFLICT: {
    code: "CF-VAL-4009",
    description: "Resource conflict",
  },
  UNPROCESSABLE_ENTITY: {
    code: "CF-VAL-4022",
    description: "Unprocessable entity",
  },

  // Authentication Errors
  JWT_AUTHENTICATION_FAILURE: {
    code: "CF-AUTH-4001",
    description: "JWT authentication failure",
  },
  INVALID_REFRESH_TOKEN: {
    code: "CF-AUTH-4002",
    description: "Refresh token is invalid or expired",
  },
  INVALID_API_KEY: {
    code: "CF-AUTH-4003",
    description: "Invalid API key",
  },
  API_KEY_EXPIRED: {
    code: "CF-AUTH-4004",
    description: "API key has expired",
  },
  USER_DISABLED: {
    code: "CF-AUTH-4005",
    description: "User account is disabled",
  },
  MISSING_AUTHORIZATION_HEADER: {
    code: "CF-AUTH-4006",
    description: "Authorization header is missing",
  },
  MISSING_USER_ID: {
    code: "CF-AUTH-4007",
    description: "User ID is missing",
  },
  UNAUTHORIZED: {
    code: "CF-AUTH-4008",
    description: "Unauthorized access",
  },
  FORBIDDEN: {
    code: "CF-AUTH-4009",
    description: "Access forbidden",
  },

  // Rate Limiting
  TOO_MANY_REQUESTS: {
    code: "CF-RATE-429",
    description: "Too many requests",
  },

  // GitHub Integration Errors
  GITHUB_API_ERROR: {
    code: "CF-GH-5001",
    description: "GitHub API error",
  },
  GITHUB_INSTALLATION_NOT_FOUND: {
    code: "CF-GH-4001",
    description: "GitHub installation not found",
  },
  GITHUB_REPOSITORY_NOT_FOUND: {
    code: "CF-GH-4002",
    description: "GitHub repository not found",
  },
  GITHUB_WEBHOOK_VERIFICATION_FAILED: {
    code: "CF-GH-4003",
    description: "GitHub webhook verification failed",
  },
  GITHUB_NOT_COLLABORATOR: {
    code: "CF-GH-4004",
    description: "User is not a collaborator on the repository",
  },
  GITHUB_BRANCH_NOT_FOUND: {
    code: "CF-GH-4005",
    description: "Branch not found",
  },
  GITHUB_PR_NOT_FOUND: {
    code: "CF-GH-4006",
    description: "Pull request not found",
  },
  GITHUB_INSTALLATION_ERROR: {
    code: "CF-GH-4007",
    description: "GitHub App installation error",
  },

  // Optimization Errors
  OPTIMIZATION_NOT_FOUND: {
    code: "CF-OPT-4001",
    description: "Optimization not found",
  },
  OPTIMIZATION_ALREADY_EXISTS: {
    code: "CF-OPT-4002",
    description: "Optimization already exists",
  },
  OPTIMIZATION_LIMIT_EXCEEDED: {
    code: "CF-OPT-4003",
    description: "Optimization limit exceeded",
  },
  OPTIMIZATION_REJECTED: {
    code: "CF-OPT-4004",
    description: "Optimization request was rejected",
  },
  OPTIMIZATION_APPROVAL_ERROR: {
    code: "CF-OPT-4005",
    description: "Error checking approval status",
  },

  // Subscription Errors
  SUBSCRIPTION_NOT_FOUND: {
    code: "CF-SUB-4001",
    description: "Subscription not found",
  },
  SUBSCRIPTION_EXPIRED: {
    code: "CF-SUB-4002",
    description: "Subscription has expired",
  },
  SUBSCRIPTION_LIMIT_REACHED: {
    code: "CF-SUB-4003",
    description: "Subscription limit reached",
  },
  SUBSCRIPTION_INACTIVE: {
    code: "CF-SUB-4004",
    description: "Subscription is not active",
  },

  // Database Errors
  DATABASE_CONNECTION_ERROR: {
    code: "CF-DB-5001",
    description: "Database connection error",
  },
  DATABASE_QUERY_ERROR: {
    code: "CF-DB-5002",
    description: "Database query error",
  },
  DUPLICATE_KEY_ERROR: {
    code: "CF-DB-4001",
    description: "Duplicate key error",
  },

  // External Service Errors
  EXTERNAL_SERVICE_ERROR: {
    code: "CF-EXT-5001",
    description: "External service error",
  },
  EXTERNAL_SERVICE_TIMEOUT: {
    code: "CF-EXT-5002",
    description: "External service timeout",
  },
} as const

export type AppErrorCodeKey = keyof typeof AppErrorCode
