// TODO(SQCY-2778): temporary localStorage-backed mock. Replace get/set/clear
// with the real backend endpoints in the next iteration — the React Query
// hooks in src/hooks/useUserMood.ts consume the same { data } shape, so the
// swap should be contained to this file.
import { type UserMood } from 'src/types/userMood';

const storageKey = (userId: number) => `huMood:${userId}`;

const readFromStorage = (userId: number): UserMood | null => {
  const raw = localStorage.getItem(storageKey(userId));
  if (raw == null) return null;
  // Guard against corrupted/legacy values so a bad entry reads as "no mood"
  // instead of throwing. The real endpoint won't need this.
  try {
    return JSON.parse(raw) as UserMood;
  } catch {
    return null;
  }
};

export const getCurrentMood = (userId: number) =>
  Promise.resolve({ data: readFromStorage(userId) });

export const setMood = (userId: number, mood: UserMood) => {
  localStorage.setItem(storageKey(userId), JSON.stringify(mood));
  return Promise.resolve({ data: mood });
};

export const clearMood = (userId: number) => {
  localStorage.removeItem(storageKey(userId));
  return Promise.resolve({ data: null });
};
