import { atom } from 'jotai';
import { atomWithStorage, createJSONStorage } from 'jotai/utils';

const STORAGE_KEY = 'cinny_recent_searches_v1';
const MAX_RECENT_SEARCHES = 10;

// Internal atom persists as a plain string[] (JSON-serializable).
const internalAtom = atomWithStorage<string[]>(
  STORAGE_KEY,
  [],
  createJSONStorage(() => localStorage),
);

/**
 * Global atom: string[] of the most recent distinct, non-empty search terms.
 * Most-recent first, deduped, capped at MAX_RECENT_SEARCHES.
 * Backed by localStorage so recent searches survive page refreshes.
 */
export const recentSearchesAtom = atom(
  (get): string[] => get(internalAtom),
  (_get, set, updater: string[] | ((prev: string[]) => string[])) => {
    set(internalAtom, (prev) => {
      const prevList = Array.isArray(prev) ? prev : [];
      const next = typeof updater === 'function' ? updater(prevList) : updater;
      return next;
    });
  },
);

/**
 * Prepend a search term: dedupes (case-sensitive), drops empties, caps at 10.
 */
export const addRecentSearch = (prev: string[], term: string): string[] => {
  const trimmed = term.trim();
  if (!trimmed) return prev;
  const withoutDupe = prev.filter((t) => t !== trimmed);
  return [trimmed, ...withoutDupe].slice(0, MAX_RECENT_SEARCHES);
};

/**
 * Wipe persisted recent search terms. Called on logout — search queries are
 * user PII and must not survive a session on a shared device.
 */
export const clearRecentSearches = (): void => {
  try {
    localStorage.removeItem(STORAGE_KEY);
  } catch {
    /* localStorage unavailable — nothing to clear */
  }
};
