import { NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { errorResponse } 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) return NextResponse.json({ error: 'Missing id' }, { status: 400 });
      await clearDuplicate(id, ctx.portId);
      return NextResponse.json({ ok: true });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
