import { NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { errorResponse, ValidationError } from '@/lib/errors';
import { clearDuplicate } from '@/lib/services/expense-dedup.service';

export const POST = withAuth(
  withPermission('expenses', 'edit', async (_req, ctx, params) => {
    try {
      const id = params.id;
      if (!id) throw new ValidationError('id is required');
      await clearDuplicate(id, ctx.portId, ctx.userId);
      return new NextResponse(null, { status: 204 });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
