/**
 * Redux Store Configuration
 * Configures Redux store with persistence and middleware
 */

import { configureStore, combineReducers } from '@reduxjs/toolkit';
import {
  persistStore,
  persistReducer,
  FLUSH,
  REHYDRATE,
  PAUSE,
  PERSIST,
  PURGE,
  REGISTER,
} from 'redux-persist';
import storage from 'redux-persist/lib/storage'; // localStorage for web

// Import slices
import authReducer from './slices/authSlice';
import messagingReducer from './slices/messagingSlice';
import callsReducer from './slices/callsSlice';

// Combine reducers
const rootReducer = combineReducers({
  auth: authReducer,
  messaging: messagingReducer,
  calls: callsReducer,
});

// Persist configuration
const persistConfig = {
  key: 'aethex-root',
  version: 1,
  storage,
  whitelist: ['auth'], // Only persist auth state
};

const persistedReducer = persistReducer(persistConfig, rootReducer);

// Configure store
export const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: {
        ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
      },
    }),
});

export const persistor = persistStore(store);

// Types
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
