/**
 * Queue Constants
 * 
 * Contains all queue-related constants including:
 * - Queue types and identifiers
 * - Polling configuration
 * - Retry settings
 * - DLQ configuration
 */

export const QUEUE_CONSTANTS = {
  // Polling Configuration
  POLL_INTERVAL_SECONDS: 10, // How often to poll each queue (in seconds via cron)
  MAX_MESSAGES_PER_POLL: 10, // Maximum messages to receive per poll
  LONG_POLL_WAIT_TIME_SECONDS: 20, // Long polling wait time
  
  // Visibility Timeouts (different per queue type)
  COMMUNICATION_VISIBILITY_TIMEOUT_SECONDS: 300, // 5 minutes - fast communications
  HEAVY_PROCESSING_VISIBILITY_TIMEOUT_SECONDS: 600, // 10 minutes - slow PDF generation
  EVENT_VISIBILITY_TIMEOUT_SECONDS: 600, // 10 minutes - triggers bulk registration
  
  MESSAGE_RETENTION_SECONDS: 1209600, // 14 days - maximum SQS allows

  // Retry Configuration
  MAX_RECEIVE_COUNT: 3, // After 3 failed attempts, move to DLQ
  RETRY_BACKOFF_MULTIPLIER: 2, // Exponential backoff multiplier
  INITIAL_RETRY_DELAY_MS: 1000, // Initial retry delay (1 second)

  // Message Size
  MAX_MESSAGE_SIZE_BYTES: 262144, // 256KB - SQS limit

  // Queue Types
  QUEUE_TYPES: {
    COMMUNICATION: 'communication', // Email, WhatsApp, SMS - fast processing (~1-3 seconds)
    HEAVY_PROCESSING: 'heavy-processing', // Invoice generation, PDF creation - slow processing (~10-30 seconds)
    EVENT: 'event', // Scheduled events (EventBridge -> SQS), e.g. join-link generation
  },

  // Communication sub-types (identifies what type of communication within the communication queue)
  COMMUNICATION_TYPES: {
    EMAIL: 'email',
    BULK_EMAIL: 'bulk-email', // Provider-side batch (ZeptoMail /batch) — many recipients per message
    WHATSAPP: 'whatsapp',
    BULK_WHATSAPP: 'bulk-whatsapp', // WATI bulk template send — many recipients per message
    SMS: 'sms',
  },

  // Max recipients per bulk-email queue message (keeps each SQS message well under the
  // 256 KB limit and within the provider batch size).
  BULK_EMAIL_BATCH_SIZE: 100,

  // Max recipients per bulk-whatsapp queue message. Same purpose as BULK_EMAIL_BATCH_SIZE —
  // keep each SQS message well under the 256 KB limit — plus a second constraint the email
  // path doesn't have: the processor walks its recipients in WHATSAPP_BULK_SETTINGS.BATCH_SIZE
  // chunks with a delay between each, so an un-chunked message could outlive
  // COMMUNICATION_VISIBILITY_TIMEOUT_SECONDS and be redelivered mid-send.
  BULK_WHATSAPP_BATCH_SIZE: 50,

  // Heavy processing sub-types
  HEAVY_PROCESSING_TYPES: {
    INVOICE: 'invoice', // Post-payment regular invoices and pre-payment proforma invoices
  },

  // Event sub-types (scheduled events delivered via EventBridge -> event queue)
  EVENT_TYPES: {
    JOIN_LINK_GENERATION: 'join-link-generation', // Scheduled bulk registration → per-registrant join links
    SESSION_COMMUNICATION_TRIGGER: 'session-communication-trigger', // Scheduled Welcome / Program-completion send
  },

  // Job Status (for tracking)
  JOB_STATUS: {
    PENDING: 'pending',
    PROCESSING: 'processing',
    COMPLETED: 'completed',
    FAILED: 'failed',
    RETRYING: 'retrying',
    DEAD_LETTER: 'dead_letter',
  },

  // Error Types
  ERROR_TYPES: {
    NETWORK_ERROR: 'NETWORK_ERROR',
    VALIDATION_ERROR: 'VALIDATION_ERROR',
    PROCESSING_ERROR: 'PROCESSING_ERROR',
    RATE_LIMIT_ERROR: 'RATE_LIMIT_ERROR',
    SERVICE_UNAVAILABLE: 'SERVICE_UNAVAILABLE',
  },
} as const;

// Type exports for TypeScript
export type QueueType = typeof QUEUE_CONSTANTS.QUEUE_TYPES[keyof typeof QUEUE_CONSTANTS.QUEUE_TYPES];
export type JobStatus = typeof QUEUE_CONSTANTS.JOB_STATUS[keyof typeof QUEUE_CONSTANTS.JOB_STATUS];
export type ErrorType = typeof QUEUE_CONSTANTS.ERROR_TYPES[keyof typeof QUEUE_CONSTANTS.ERROR_TYPES];
