export const WALKTHROUGH_SEEN_KEY = 'sut.walkthroughSeen.v1';

export interface TourStep {
  target: string;
  placement: 'top' | 'bottom' | 'left' | 'right';
  disableBeacon: boolean;
  content: string;
  route: string;
}

export const walkthroughSteps: TourStep[] = [
  {
    target: '[data-tour="main-navigation"]',
    placement: 'bottom',
    disableBeacon: true,
    route: '/dashboard',
    content:
      'Welcome to your Fellowship command center. Use this navigation to move between quests, the map, and your inventory.',
  },
  {
    target: '[data-tour="quests-overview"]',
    placement: 'top',
    disableBeacon: true,
    route: '/quests',
    content:
      'This is where quests are managed. Create a new quest, track progress, and keep your party aligned on objectives.',
  },
  {
    target: '[data-tour="dialogue-panel"]',
    placement: 'left',
    disableBeacon: true,
    route: '/map?tourCharacterPanel=1',
    content:
      'On the map, talk with LOTR characters here. Each conversation can reveal hints, trade context, and negotiation opportunities.',
  },
  {
    target: '[data-tour="bargain-ledger"]',
    placement: 'left',
    disableBeacon: true,
    route: '/map?tourCharacterPanel=1',
    content:
      'This ledger drives bargaining. Start with a seller item, send offers, and adjust based on negotiation status and your gold.',
  },
  {
    target: '[data-tour="inventory-stats"]',
    placement: 'bottom',
    disableBeacon: true,
    route: '/inventory',
    content:
      'Your inventory records bargaining performance. Best bargain and average savings show how skillfully you trade across Middle-earth.',
  },
];

const getStorage = (storage?: Storage): Storage | null => {
  if (storage) {
    return storage;
  }

  if (typeof window !== 'undefined' && window.localStorage) {
    return window.localStorage;
  }

  return null;
};

export const hasSeenWalkthrough = (storage?: Storage): boolean => {
  const safeStorage = getStorage(storage);
  if (!safeStorage) {
    return false;
  }

  return safeStorage.getItem(WALKTHROUGH_SEEN_KEY) === 'true';
};

export const markWalkthroughSeen = (storage?: Storage): void => {
  const safeStorage = getStorage(storage);
  if (!safeStorage) {
    return;
  }

  safeStorage.setItem(WALKTHROUGH_SEEN_KEY, 'true');
};

export const clearWalkthroughSeen = (storage?: Storage): void => {
  const safeStorage = getStorage(storage);
  if (!safeStorage) {
    return;
  }

  safeStorage.removeItem(WALKTHROUGH_SEEN_KEY);
};