import { NextResponse } from 'next/server';

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

export const GET = withAuth(
  withPermission('documents', 'view', async (req, ctx, params) => {
    try {
      const events = await listDocumentEvents(params.id!, ctx.portId);
      return NextResponse.json({ data: events });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
