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

interface DashboardState {
  data: DashboardData | null;
  isLoading: boolean;
  lastUpdated: string | null;
  refreshDashboard: () => Promise<void>;
  setData: (data: DashboardData) => void;
}

export const useDashboardStore = create<DashboardState>()(
  persist(
    (set) => ({
      data: null,
      isLoading: false,
      lastUpdated: null,

      refreshDashboard: async () => {
        set({ isLoading: true });
        try {
          const mockData: DashboardData = {
            exposureSummary: { total: 0, unresolved: 0, critical: 0 },
            spamStats: { blockedToday: 0, blockedTotal: 0, spamScore: 0 },
            voiceProtectionStatus: { isMonitoring: false, profilesEnrolled: 0, lastAnalysis: '' },
          };
          set({
            data: mockData,
            isLoading: false,
            lastUpdated: new Date().toISOString(),
          });
        } catch {
          set({ isLoading: false });
        }
      },

      setData: (data) => set({ data, lastUpdated: new Date().toISOString() }),
    }),
    {
      name: '@shieldai_dashboard',
      storage: createJSONStorage(() => AsyncStorage),
    }
  )
);
