import { NextResponse } from 'next/server';

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

// POST /api/v1/berths/[id]/restore
// Post-audit F5: reverses an archive. No body. Audit-logged.
export const POST = withAuth(
  withPermission('berths', 'edit', async (_req, ctx, params) => {
    try {
      await restoreBerth(params.id!, ctx.portId, {
        userId: ctx.userId,
        portId: ctx.portId,
        ipAddress: ctx.ipAddress,
        userAgent: ctx.userAgent,
      });
      return new NextResponse(null, { status: 204 });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
