import { atom } from 'jotai'; export type BackendUser = { id: string; email: string | null; isGuest: boolean; profileComplete: boolean; }; export type GuestSession = { id: string; isGuest: true; createdAt: string; }; export const GUEST_SESSION_STORAGE_KEY = 'guest-session'; const readGuestSession = (): GuestSession | null => { if (typeof window === 'undefined') return null; const raw = localStorage.getItem(GUEST_SESSION_STORAGE_KEY); if (!raw) return null; try { const parsed = JSON.parse(raw) as GuestSession; if (!parsed?.id || parsed?.isGuest !== true) return null; return parsed; } catch { return null; } }; export const backendUserAtom = atom(null); export const isAuthenticatedAtom = atom((get) => { return !!get(backendUserAtom); }); export const guestSessionAtom = atom(readGuestSession()); export const guestSessionHydratedAtom = atom(false); export const isGuestAtom = atom((get) => !!get(guestSessionAtom)); export const effectiveUserIdAtom = atom((get) => { const user = get(backendUserAtom); if (user?.id) return user.id; const guest = get(guestSessionAtom); return guest?.id ?? null; }); export const authInitialCheckDoneAtom = atom(false);