import { NextResponse } from 'next/server';

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

/**
 * GET /api/v1/admin/onboarding/status — returns the resolved checklist
 * state for the caller's port. Drives the topbar discoverability banner
 * + dashboard onboarding tile + the admin checklist page summary.
 *
 * Permission-gated on `admin.manage_settings` to mirror the rest of the
 * admin surface; non-admin users never see the banner / tile anyway.
 */
export const GET = withAuth(
  withPermission('admin', 'manage_settings', async (_req, ctx) => {
    try {
      const status = await resolveOnboardingStatus(ctx.portId);
      return NextResponse.json({ data: status });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
