/**
 * PostgreSQL Exception Codes and Prefixes
 *
 * This module defines standard PostgreSQL error codes and code prefixes
 * used for identifying specific types of database errors. These codes
 * follow the SQLSTATE standard (5-character codes) defined in the
 * PostgreSQL documentation.
 *
 * Reference: https://www.postgresql.org/docs/current/errcodes-appendix.html
 *
 * Purpose:
 * - Identify specific database errors for appropriate error handling
 * - Map database errors to meaningful HTTP status codes
 * - Provide consistent error categorization across the application
 *
 * @example
 * // Check if error is a unique constraint violation
 * if (error.code === PgExceptionCodes.UNIQUE_VIOLATION) {
 *   throw new BadRequestException('Duplicate entry');
 * }
 */

/**
 * PostgreSQL SQLSTATE error codes
 *
 * These 5-character codes represent specific error conditions in PostgreSQL.
 * The first two characters represent the error class, the last three
 * represent the specific error within that class.
 *
 * Format: SSXXX (SS = class, XXX = subclass)
 *
 * @example '23505' = Class 23 (Integrity Constraint Violation), Subclass 505 (Unique Violation)
 */
export const PgExceptionCodes = {
  /**
   * Class 23: Integrity Constraint Violation
   * Generic integrity constraint violation (parent class)
   *
   * Use cases: Any constraint violation not covered by specific subclasses
   * @example Attempting to insert NULL into NOT NULL column
   */
  INTEGRITY_CONSTRAINT_VIOLATION: '23000',

  /**
   * Subclass 23502: Not Null Violation
   * Violation of NOT NULL constraint
   *
   * Triggered when: Inserting or updating with NULL in NOT NULL column
   * @example INSERT INTO users (id, email) VALUES (1, NULL);
   */
  NOT_NULL_VIOLATION: '23502',

  /**
   * Subclass 23505: Unique Violation
   * Violation of UNIQUE constraint
   *
   * Triggered when: Inserting or updating with duplicate unique key value
   * @example INSERT INTO users (email) VALUES ('existing@email.com');
   */
  UNIQUE_VIOLATION: '23505',

  /**
   * Subclass 23503: Foreign Key Violation
   * Violation of FOREIGN KEY constraint
   *
   * Triggered when:
   * - Inserting child row with non-existent parent key
   * - Updating foreign key to non-existent value
   * - Deleting parent row with existing child references
   * @example INSERT INTO orders (user_id) VALUES (999) -- user_id 999 doesn't exist
   */
  FOREIGN_CONSTRAINT_VIOLATION: '23503',
};

/**
 * PostgreSQL error code prefixes (first two characters)
 *
 * These represent error classes that group related error conditions.
 * Useful for categorizing errors when only class-level handling is needed.
 *
 * @example '22' = Data Exception (all data-related errors)
 */
export const PgExceptionPrefixes = {
  /**
   * Class 22: Data Exception
   *
   * Covers various data-related errors including:
   * - Invalid data type conversions
   * - Numeric value out of range
   * - String data length mismatches
   * - Invalid datetime formats
   *
   * Common subclasses:
   * - 22001: String data right truncation
   * - 22003: Numeric value out of range
   * - 22007: Invalid datetime format
   * - 22012: Division by zero
   *
   * @example '22P02' = Invalid text representation (cannot convert string to number)
   */
  DATA_ERROR: '22',
};
