import { NextResponse } from 'next/server';

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

export const GET = withAuth(
  withPermission('documents', 'view', async (_req, ctx, params) => {
    try {
      const id = params.id!;
      const [workflow, signers, events] = await Promise.all([
        getDocumentById(id, ctx.portId),
        listDocumentSigners(id, ctx.portId),
        listDocumentEvents(id, ctx.portId),
      ]);
      return NextResponse.json({ data: { workflow, signers, events } });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
