import { create } from 'zustand';
import { persist, devtools } from 'zustand/middleware';
// NOTE: Domain data actions migrated to React Query
// Removed imports: undoRedo, stateMiddleware, stateNormalization utilities
// Removed types: NormalizedState, WithUndoRedo, LibraryItem, PaginatedResponse, ApiError (for store)

// FE-TYPE-011: Fully typed store interfaces
// NOTE: Domain data (items, favorites, pagination) migrated to React Query
// Only UI state remains in Zustand store
export interface LibraryState {
  filters: {
    type?: string;
    search?: string;
  };
}

export interface LibraryActions {
  // NOTE: Domain data actions (fetchItems, fetchFavorites, uploadFile, toggleFavorite, deleteItem, clearItems)
  // have been migrated to React Query. See useLibraryActions() in storeSelectors.ts
  setFilters: (filters: { type?: string; search?: string }) => void;
}

// FE-TYPE-011: Export store type for reuse
// NOTE: No longer using WithUndoRedo wrapper since we removed domain data
export type LibraryStore = LibraryState & LibraryActions;

export const useLibraryStore = create<LibraryStore>()(
  devtools(
    persist(
      (set) => ({
        // État initial - Only UI state remains
        filters: {},

        // Actions
        // NOTE: Domain data actions (fetchItems, fetchFavorites, uploadFile, toggleFavorite, deleteItem, clearItems)
        // have been migrated to React Query. See useLibraryActions() in storeSelectors.ts
        setFilters: (filters) => {
          set({ filters });
          // NOTE: React Query hooks will automatically refetch when filters change
          // Components using useLibraryItems() with filters will refetch automatically
        },
      }),
      {
        name: 'library-storage',
        partialize: (state) => ({
          // FE-STATE-001: Persist filters for offline support
          // NOTE: favorites removed - React Query handles persistence
          filters: state.filters,
        }),
      },
    ),
    {
      // FE-STATE-007: Enable Redux DevTools for debugging
      name: 'LibraryStore',
      enabled: import.meta.env.DEV,
    },
  ),
);
