export async function saveSessionChoice(step: string, choice: unknown) {
  try {
    const searchParams =
      typeof window !== 'undefined' ? new URLSearchParams(window.location.search) : null;
    const utmAd = searchParams?.get('utm_ad') ?? undefined;

    const response = await fetch('/api/session', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ step, choice, utmAd }),
    });

    if (!response.ok) {
      console.error('Failed to persist onboarding choice', await response.text());
    }
  } catch (error) {
    console.error('Failed to persist onboarding choice', error);
  }
}
