/**
 * AWS SQS Configuration
 * 
 * Provides configuration for SQS client and queue URLs
 */

import { SQSClientConfig } from '@aws-sdk/client-sqs';

/**
 * Get SQS client configuration from environment variables
 */
export const getSqsClientConfig = (): SQSClientConfig => {
  const region = process.env.AWS_SQS_REGION;
  const accessKeyId = process.env.AWS_SQS_ACCESS_KEY_ID;
  const secretAccessKey = process.env.AWS_SQS_SECRET_ACCESS_KEY;

  if (!region) {
    throw new Error('AWS_SQS_REGION environment variable is not set');
  }

  // Support both explicit credentials and IAM role
  const config: SQSClientConfig = {
    region,
  };

  if (accessKeyId && secretAccessKey) {
    config.credentials = {
      accessKeyId,
      secretAccessKey,
    };
  }
  // If credentials are not provided, the SDK will use IAM role or other AWS credential providers

  return config;
};

/**
 * Get all queue URLs from environment variables
 * Simplified to 2 queues: communication and heavy-processing
 */
export const getQueueUrls = () => {
  return {
    communication: process.env.AWS_SQS_COMMUNICATION_QUEUE_URL,
    heavyProcessing: process.env.AWS_SQS_HEAVY_PROCESSING_QUEUE_URL,
  };
};

/**
 * Validate that all required queue URLs are configured
 */
export const validateQueueConfiguration = (): void => {
  const queueUrls = getQueueUrls();
  const missingQueues: string[] = [];

  Object.entries(queueUrls).forEach(([key, value]) => {
    if (!value) {
      missingQueues.push(key);
    }
  });

  if (missingQueues.length > 0) {
    console.warn(
      `Warning: The following queue URLs are not configured: ${missingQueues.join(', ')}. ` +
      `Queue functionality for these types will be disabled.`
    );
  }
};

/**
 * Feature flags for enabling/disabling specific queues
 * Simplified to 2 queues
 */
export const getQueueFeatureFlags = () => {
  return {
    communicationQueueEnabled: process.env.ENABLE_COMMUNICATION_QUEUE?.toLowerCase() === 'true',
    heavyProcessingQueueEnabled: process.env.ENABLE_HEAVY_PROCESSING_QUEUE?.toLowerCase() === 'true',
  };
};
