import { useCallback } from "react";
import { useSettingsStore } from "@/stores/settings";
import * as settingsService from "@/services/settings";
import type { Settings } from "@/lib/types";

export function useSettings() {
  const { settings, setSettings } = useSettingsStore();

  // Note: Backend settings sync removed until Week 3 (Episteme integration)
  // Settings are persisted to localStorage via zustand/persist

  const updateSettings = useCallback(
    async (updates: Partial<Settings>) => {
      const newSettings = { ...settings, ...updates };
      setSettings(updates);

      // Sync to backend (will persist in Week 3)
      try {
        await settingsService.updateSettings(newSettings);
      } catch {
        // Silently fail - localStorage is the source of truth for now
        // Backend persistence will be added in Week 3
      }
    },
    [settings, setSettings],
  );

  return {
    settings,
    updateSettings,
  };
}
