import { NextResponse } from 'next/server';

import { requireSuperAdmin, withAuth } from '@/lib/api/helpers';
import { errorResponse, ValidationError } from '@/lib/errors';
import { retryJob } from '@/lib/services/system-monitoring.service';
import { QUEUE_CONFIGS, type QueueName } from '@/lib/queue';

export const POST = withAuth(async (_req, ctx, params) => {
  try {
    requireSuperAdmin(ctx, 'admin.queues.job.retry');

    const queueName = params['queueName'];
    const jobId = params['jobId'];
    if (!queueName || !jobId) {
      throw new ValidationError('queueName and jobId are required');
    }
    const validQueues = Object.keys(QUEUE_CONFIGS) as QueueName[];
    if (!validQueues.includes(queueName as QueueName)) {
      throw new ValidationError('Invalid queue name');
    }

    await retryJob(queueName as QueueName, jobId, ctx.userId);
    return NextResponse.json({ data: { success: true } });
  } catch (error) {
    return errorResponse(error);
  }
});
