import { NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { errorResponse } from '@/lib/errors';
import { removeDocumentWatcher } from '@/lib/services/documents.service';

export const DELETE = withAuth(
  withPermission('documents', 'edit', async (_req, ctx, params) => {
    try {
      await removeDocumentWatcher(params.id!, ctx.portId, params.userId!, {
        userId: ctx.userId,
        portId: ctx.portId,
        ipAddress: ctx.ipAddress,
        userAgent: ctx.userAgent,
      });
      return NextResponse.json({ data: { ok: true } });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
