import { NextResponse } from 'next/server';

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

export const DELETE = withAuth(async (_req, ctx, params) => {
  try {
    if (!ctx.isSuperAdmin) {
      return NextResponse.json({ error: 'Insufficient permissions' }, { status: 403 });
    }

    const queueName = params['queueName'];
    const jobId = params['jobId'];
    if (!queueName || !jobId) {
      return NextResponse.json({ error: 'Missing parameters' }, { status: 400 });
    }
    const validQueues = Object.keys(QUEUE_CONFIGS) as QueueName[];
    if (!validQueues.includes(queueName as QueueName)) {
      return NextResponse.json({ error: 'Invalid queue name' }, { status: 400 });
    }

    await deleteJob(queueName as QueueName, jobId, ctx.userId);
    return new NextResponse(null, { status: 204 });
  } catch (error) {
    return errorResponse(error);
  }
});
