import { Queue, QueueEvents, type Job } from 'bullmq';

import { redisConnection } from './redisConnection.js';

export type AdoptionReportJobData = {
  birdId: string;
  workspaceId: number;
  transferCode: string;
  printFriendly: boolean;
};

export type AdoptionReportJobResult = {
  pdfBase64: string;
};

export const adoptionReportQueueName = 'adoption-reports';

export const adoptionReportQueue = new Queue<AdoptionReportJobData, AdoptionReportJobResult>(adoptionReportQueueName, {
  connection: redisConnection,
  defaultJobOptions: {
    attempts: 2,
    backoff: {
      type: 'exponential',
      delay: 10_000,
    },
    removeOnComplete: 50,
    removeOnFail: 500,
  },
});

export const adoptionReportQueueEvents = new QueueEvents(adoptionReportQueueName, {
  connection: redisConnection,
});

export const enqueueAdoptionReportJob = (
  data: AdoptionReportJobData,
): Promise<Job<AdoptionReportJobData, AdoptionReportJobResult>> => adoptionReportQueue.add('render-adoption-report', data);

export const closeAdoptionReportQueue = async () => {
  await adoptionReportQueue.close();
  await adoptionReportQueueEvents.close();
};
