import { NextRequest, NextResponse } from 'next/server';
import { withAuth } from '@/lib/api-guards';

export const GET = withAuth(async (req, { prisma, params }) => {
  const unwrappedParams = await params;
  const { key } = unwrappedParams;

  if (!key) {
    return NextResponse.json({
      success: false,
      error: {
        code: 'MISSING_KEY',
        metadata: {}
      }
    }, { status: 400 });
  }

  const job = await prisma.salesImportJob.findUnique({
    where: { idempotencyKey: key }
  });

  return NextResponse.json({
    success: true,
    code: 'STATUS_CHECKED',
    metadata: {
      idempotencyKey: key,
      status: job ? job.status : 'PROCESSING',
      errorMessage: job ? job.errorMessage : null
    }
  });
}, ['admin', 'analyst', 'commercial_leader']);
