import { setOnboardingStep, isOnboardingStep } from "../../../db/profile";

/**
 * PATCH /api/profile/me/onboarding-step
 *
 * Setzt den Onboarding-Stand des eingeloggten Users.
 * Werte: "welcome" | "nickname" | "block" | "done"
 *
 * Body: { step: "nickname" | "block" | "done" }
 *
 * Das Frontend ruft das nach jedem abgeschlossenen Schritt auf:
 *   Welcome-Screen "Los geht's"  → step="nickname"
 *   Nickname-Save                → step="block"
 *   Block-Aktivierung done       → step="done"
 */
export default defineEventHandler(async (event) => {
  const user = await requireUser(event);
  const body = await readBody(event);
  const { step } = body as { step?: unknown };

  if (!isOnboardingStep(step)) {
    throw createError({ statusCode: 400, statusMessage: "invalid_step" });
  }

  const updated = await setOnboardingStep(user.id, step);
  return { success: true, data: { onboardingStep: updated.onboardingStep } };
});
