import { describe, expect, it } from 'vitest';
import {
  getRoleTourStorageKey,
  getWelcomeTourStorageKey,
  normalizeRoleForTour,
  pickGuidedTour,
  readSeenRoleTours,
  writeSeenRoleTours,
} from './guided-tour';

describe('guided-tour storage keys', () => {
  it('creates stable keys by user id', () => {
    expect(getWelcomeTourStorageKey('u1')).toBe('nxtgauge_tour_welcome_seen_u1');
    expect(getRoleTourStorageKey('u1')).toBe('nxtgauge_tour_roles_seen_u1');
  });
});

describe('normalizeRoleForTour', () => {
  it('normalizes spacing and case', () => {
    expect(normalizeRoleForTour(' job seeker ')).toBe('JOB_SEEKER');
    expect(normalizeRoleForTour('make-up artist')).toBe('MAKE_UP_ARTIST');
  });
});

describe('role tour serialization', () => {
  it('reads and writes role sets without duplicates', () => {
    const serialized = writeSeenRoleTours(['customer', 'CUSTOMER', 'job-seeker']);
    expect(serialized).toBe('CUSTOMER,JOB_SEEKER');

    const set = readSeenRoleTours(serialized);
    expect(set.has('CUSTOMER')).toBe(true);
    expect(set.has('JOB_SEEKER')).toBe(true);
    expect(set.size).toBe(2);
  });
});

describe('pickGuidedTour', () => {
  it('shows welcome tour first for known user', () => {
    const next = pickGuidedTour({
      userId: 'u1',
      activeRole: 'USER',
      welcomeTourSeen: false,
      seenRoleTours: new Set(),
    });
    expect(next).toBe('welcome');
  });

  it('shows role-approved tour once when active role is non-user', () => {
    const next = pickGuidedTour({
      userId: 'u1',
      activeRole: 'photographer',
      welcomeTourSeen: true,
      seenRoleTours: new Set(['CUSTOMER']),
    });
    expect(next).toBe('role-approved');
  });

  it('does not show role-approved tour for already seen roles', () => {
    const next = pickGuidedTour({
      userId: 'u1',
      activeRole: 'TUTOR',
      welcomeTourSeen: true,
      seenRoleTours: new Set(['TUTOR']),
    });
    expect(next).toBeNull();
  });
});
