import { Injectable, signal } from '@angular/core';
import { WizardSession } from './api.service';

/**
 * Holds the active wizard session token + answers across the wizard steps and
 * the results page (the server is the source of truth; this is the local view).
 */
@Injectable({ providedIn: 'root' })
export class WizardStateService {
  readonly token = signal<string | null>(null);
  readonly answers = signal<Partial<WizardSession>>({});

  setSession(s: WizardSession): void {
    this.token.set(s.token);
    this.answers.update((a) => ({ ...a, ...s }));
  }

  patch(data: Partial<WizardSession>): void {
    this.answers.update((a) => ({ ...a, ...data }));
  }

  reset(): void {
    this.token.set(null);
    this.answers.set({});
  }
}
