import { useCallback } from 'react';
import { safeStorage } from '../../../core/utils/safe-storage';

export interface SettingsState {
  darkMode: boolean;
  unitSystem: 'imperial' | 'metric';
  notifications: {
    email: boolean;
    push: boolean;
    maintenance: boolean;
  };
}

const SETTINGS_STORAGE_KEY = 'motovaultpro-mobile-settings';

export const useSettingsPersistence = () => {
  const loadSettings = useCallback((): SettingsState | null => {
    try {
      const stored = safeStorage.getItem(SETTINGS_STORAGE_KEY);
      return stored ? JSON.parse(stored) : null;
    } catch (error) {
      console.error('[Settings] Error loading settings:', error);
      return null;
    }
  }, []);

  const saveSettings = useCallback((settings: SettingsState) => {
    try {
      safeStorage.setItem(SETTINGS_STORAGE_KEY, JSON.stringify(settings));
    } catch (error) {
      console.error('[Settings] Error saving settings:', error);
    }
  }, []);

  return {
    loadSettings,
    saveSettings,
  };
};