require('dotenv').config();

const parseNumber = (value, fallback) => {
  const parsed = Number(value);
  return Number.isNaN(parsed) ? fallback : parsed;
};

const retryDelayMs = parseNumber(process.env.RABBITMQ_RETRY_DELAY_MS, 6000);

const exchangeName = process.env.RABBITMQ_EXCHANGE || 'ai.story.exchange';

const config = {
  url: process.env.RABBITMQ_URL || 'amqp://guest:guest@localhost:5672',
  prefetch: parseNumber(process.env.RABBITMQ_PREFETCH, 1),
  retry: {
    maxAttempts: parseNumber(process.env.RABBITMQ_MAX_RETRY_ATTEMPTS, 5),
    delayMs: retryDelayMs,
  },
  exchange: {
    name: exchangeName,
    type: process.env.RABBITMQ_EXCHANGE_TYPE || 'direct',
    routingKeys: {
      story: process.env.RABBITMQ_STORY_ROUTING_KEY || 'story.generate',
      retry: process.env.RABBITMQ_RETRY_ROUTING_KEY || 'story.generate.retry',
      deadLetter: process.env.RABBITMQ_DEAD_LETTER_ROUTING_KEY || 'story.generate.dead',
    },
  },
  queues: {
    storyGeneration: {
      name: process.env.RABBITMQ_QUEUE || 'ai.story.queue',
    },
    retry: {
      name: process.env.RABBITMQ_RETRY_QUEUE || 'ai.story.retry.queue',
      messageTtl: retryDelayMs,
    },
    deadLetter: {
      name: process.env.RABBITMQ_DEAD_LETTER_QUEUE || 'ai.story.dlq',
    },
  },
};

module.exports = config;
