export type OnboardingSessionData = {
  choices: Record<string, unknown>;
  updatedAt?: string;
  userId?: string;
  utmAd?: string;
};

export const SESSION_COOKIE_NAME = 'onboarding_session';

export function parseSessionCookie(value?: string): OnboardingSessionData {
  if (!value) {
    return { choices: {} };
  }

  try {
    const parsed = JSON.parse(value) as OnboardingSessionData;
    return {
      choices: parsed.choices ?? {},
      updatedAt: parsed.updatedAt,
      userId: parsed.userId,
      utmAd: parsed.utmAd,
    };
  } catch {
    return { choices: {} };
  }
}

export function serializeSessionData(data: OnboardingSessionData): string {
  return JSON.stringify({
    choices: data.choices,
    updatedAt: data.updatedAt,
    userId: data.userId,
    utmAd: data.utmAd,
  });
}
