export const QUEUE_SERVER_ADDONS = 'server-addons' as const;
export const QUEUE_SERVER_BACKUPS = 'server-backups' as const;
export const QUEUE_SERVER_LIFECYCLE = 'server-lifecycle' as const;
export const QUEUE_SERVER_PROVISIONING = 'server-provisioning' as const;
export const QUEUE_NOTIFICATIONS = 'notifications' as const;

import { Queue } from 'bullmq';

import { validateConfig } from '@hexahost/config';

export const WORKER_QUEUES = [
  QUEUE_SERVER_ADDONS,
  QUEUE_SERVER_BACKUPS,
  QUEUE_SERVER_LIFECYCLE,
  QUEUE_SERVER_PROVISIONING,
  QUEUE_NOTIFICATIONS,
] as const;

export type WorkerQueueName = (typeof WORKER_QUEUES)[number];

let addonsQueue: Queue | null = null;

export function getAddonsQueue(): Queue {
  if (!addonsQueue) {
    const config = validateConfig();
    addonsQueue = new Queue(QUEUE_SERVER_ADDONS, {
      connection: {
        url: config.REDIS_URL,
        maxRetriesPerRequest: null,
      },
    });
  }

  return addonsQueue;
}

let backupsQueue: Queue | null = null;

export function getBackupsQueue(): Queue {
  if (!backupsQueue) {
    const config = validateConfig();
    backupsQueue = new Queue(QUEUE_SERVER_BACKUPS, {
      connection: {
        url: config.REDIS_URL,
        maxRetriesPerRequest: null,
      },
    });
  }

  return backupsQueue;
}
