import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';
import AsyncStorage from '@react-native-async-storage/async-storage';
import type { NotificationPreference } from '@/types';

/**
 * TODO: Wire updatePreferences to notificationService.updatePreferences() for production.
 * Current implementation is local-only (AsyncStorage) for offline-first MVP.
 */

interface SettingsState {
  preferences: NotificationPreference;
  isBiometricEnabled: boolean;
  updatePreferences: (prefs: Partial<NotificationPreference>) => void;
  toggleBiometric: (enabled: boolean) => void;
}

const defaultPreferences: NotificationPreference = {
  emailNotifications: true,
  pushNotifications: true,
  darkwatchAlert: true,
  spamBlocked: true,
  voiceprintAnalysis: true,
};

export const useSettingsStore = create<SettingsState>()(
  persist(
    (set) => ({
      preferences: defaultPreferences,
      isBiometricEnabled: false,

      updatePreferences: (prefs) => {
        set((state) => ({
          preferences: { ...state.preferences, ...prefs },
        }));
      },

      toggleBiometric: (enabled) => set({ isBiometricEnabled: enabled }),
    }),
    {
      name: '@shieldai_settings',
      storage: createJSONStorage(() => AsyncStorage),
    }
  )
);
