import { configureStore, combineReducers } from "@reduxjs/toolkit";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import authReducer from "./slices/authSlice";
import profileReducer from "./slices/profileSlice";
import playerReducer from "./slices/playerSlice";
import themeReducer from "./slices/themeSlice";
import homeReducer from "./slices/homeSlice";
import categoryReducer from "./slices/categorySlice";
import videoListReducer from "./slices/videoListSlice";
import reelsListReducer from "./slices/reelsListSlice";
import videoDetailReducer from "./slices/videoDetailSlice";
import musicSectionReducer from "./slices/musicSectionSlice";
import watchLaterReducer from "./slices/watchLaterSlice";
import likedContentReducer from "./slices/likedContentSlice";
import historyReducer from "./slices/historySlice";
import feedReducer from "./slices/feedSlice";
import subscriberReducer from "./slices/subscriberSlice";
import rentReducer from "./slices/rentSlice";
import packageReducer from "./slices/packageSlice";
import commentReducer from "./slices/commentSlice";
import searchReducer from "./slices/searchSlice";
import notificationReducer from "./slices/notificationSlice";
import reelQueueReducer from "./slices/reelQueueSlice";
import moreProductsReducer from "./slices/moreProductsSlice";
import marketplaceCategoryReducer from "./slices/marketplaceCategorySlice";
import marketplaceListingsReducer from "./slices/marketplaceListingsSlice";
import marketplaceListingDetailReducer from "./slices/marketplaceListingDetailSlice";
import marketplaceWishlistReducer from "./slices/marketplaceWishlistSlice";
import marketplaceMyListingsReducer from "./slices/marketplaceMyListingsSlice";
import marketplaceOrdersReducer from "./slices/marketplaceOrdersSlice";
import generalSettingReducer from "./slices/generalSettingSlice";

const rootReducer = combineReducers({
  auth: authReducer,
  profile: profileReducer,
  player: playerReducer,
  theme: themeReducer,
  home: homeReducer,
  category: categoryReducer,
  videoList: videoListReducer,
  reelsList: reelsListReducer,
  videoDetail: videoDetailReducer,
  musicSection: musicSectionReducer,
  watchLater: watchLaterReducer,
  likedContent: likedContentReducer,
  history: historyReducer,
  feed: feedReducer,
  subscriber: subscriberReducer,
  rent: rentReducer,
  packages: packageReducer,
  comments: commentReducer,
  search: searchReducer,
  notifications: notificationReducer,
  reelQueue: reelQueueReducer,
  moreProducts: moreProductsReducer,
  marketplaceCategory: marketplaceCategoryReducer,
  marketplaceListings: marketplaceListingsReducer,
  marketplaceListingDetail: marketplaceListingDetailReducer,
  marketplaceWishlist: marketplaceWishlistReducer,
  marketplaceMyListings: marketplaceMyListingsReducer,
  marketplaceOrders: marketplaceOrdersReducer,
  generalSetting: generalSettingReducer,
});

const resettableReducer = (state: ReturnType<typeof rootReducer> | undefined, action: { type: string }) => {
  if (action.type === "RESET_APP") return rootReducer(undefined, action);
  return rootReducer(state, action);
};

const persistedReducer = persistReducer(
  { key: "doliplay-root", storage, whitelist: ["theme", "auth"] },
  resettableReducer,
);

export const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({ serializableCheck: false }),
});

export const persistor = persistStore(store);
export type RootState = ReturnType<typeof rootReducer>;
export type AppDispatch = typeof store.dispatch;
