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

/**
 * TODO: Wire store operations to @shieldai/mobile-api-client for production.
 * Current implementation is local-only (AsyncStorage) for offline-first MVP.
 */

type PhoneList = { number: string; label: string }[];

interface SpamShieldState {
  callHistory: SpamRecord[];
  textHistory: SpamRecord[];
  whitelist: PhoneList;
  blacklist: PhoneList;
  isLoading: false;
  addToWhitelist: (number: string, label: string) => void;
  addToBlacklist: (number: string, label: string) => void;
  removeFromWhitelist: (number: string) => void;
  removeFromBlacklist: (number: string) => void;
}

export const useSpamShieldStore = create<SpamShieldState>()(
  persist(
    (set) => ({
      callHistory: [],
      textHistory: [],
      whitelist: [],
      blacklist: [],
      isLoading: false,

      addToWhitelist: (number, label) => {
        set((state) => ({
          whitelist: [...state.whitelist, { number, label }],
        }));
      },

      addToBlacklist: (number, label) => {
        set((state) => ({
          blacklist: [...state.blacklist, { number, label }],
        }));
      },

      removeFromWhitelist: (number) => {
        set((state) => ({
          whitelist: state.whitelist.filter((item) => item.number !== number),
        }));
      },

      removeFromBlacklist: (number) => {
        set((state) => ({
          blacklist: state.blacklist.filter((item) => item.number !== number),
        }));
      },
    }),
    {
      name: '@shieldai_spamshield',
      storage: createJSONStorage(() => AsyncStorage),
    }
  )
);
