import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { randomUUID } from 'expo-crypto';
import type { VoiceProfile, VoiceAnalysis } from '@/types';

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

interface VoicePrintState {
  profiles: VoiceProfile[];
  analyses: VoiceAnalysis[];
  isRecording: boolean;
  isLoading: boolean;
  addProfile: (name: string, relationship: string) => Promise<void>;
  removeProfile: (id: string) => void;
  startRecording: () => void;
  stopRecording: () => void;
}

export const useVoicePrintStore = create<VoicePrintState>()(
  persist(
    (set) => ({
      profiles: [],
      analyses: [],
      isRecording: false,
      isLoading: false,

      addProfile: async (name: string, relationship: string) => {
        const newProfile: VoiceProfile = {
          id: randomUUID(),
          name,
          relationship,
          enrolledAt: new Date().toISOString(),
          confidence: 0,
        };
        set((state) => ({ profiles: [...state.profiles, newProfile] }));
      },

      removeProfile: (id) => {
        set((state) => ({
          profiles: state.profiles.filter((p) => p.id !== id),
        }));
      },

      startRecording: () => set({ isRecording: true }),
      stopRecording: () => set({ isRecording: false }),
    }),
    {
      name: '@shieldai_voiceprint',
      storage: createJSONStorage(() => AsyncStorage),
    }
  )
);
