import { configureStore, type Reducer, type UnknownAction } from "@reduxjs/toolkit";
import { resetStore } from "./resetAction";
export { resetStore };

// ── CLAUDE.md defined slices ──────────────────────────────────────────────────
import homeReducer        from "@store/slices/homeSlice";
import articleReducer     from "@store/slices/articleSlice";
import categoryReducer    from "@store/slices/categorySlice";
import topicsReducer      from "@store/slices/topicsSlice";
import feedsReducer       from "@store/slices/feedsSlice";
import clipsReducer       from "@store/slices/clipsSlice";       // /shorts
import searchReducer      from "@store/slices/searchSlice";
import epaperReducer      from "@store/slices/epaperSlice";
import liveTvReducer      from "@store/slices/liveTvSlice";

// ── Feature slices ────────────────────────────────────────────────────────────
import bannerReducer        from "@store/slices/bannerSlice";
import sectionDetailReducer from "@store/slices/sectionDetailSlice";
import channelReducer       from "@store/slices/channelSlice";
import commentReducer       from "@store/slices/commentSlice";
import contentDetailReducer from "@store/slices/contentDetailSlice";
import cityReducer          from "@store/slices/citySlice";
import languageReducer      from "@store/slices/languageSlice";
import subscribeReducer     from "@store/slices/subscribeSlice";
import socialReducer        from "@store/slices/socialSlice";

// ── Auth / User slices ────────────────────────────────────────────────────────
import authReducer          from "@store/slices/authSlice";
import profileReducer       from "@store/slices/profileSlice";
import navCategoriesReducer from "@store/slices/navCategorySlice";

// Wrap every reducer so a RESET_STORE action sends `undefined` state → initial state
function withReset<S>(reducer: Reducer<S>): Reducer<S> {
  return (state: S | undefined, action: UnknownAction) =>
    action.type === resetStore.type ? reducer(undefined, action) : reducer(state, action);
}

export const store = configureStore({
  reducer: {
    // CLAUDE.md slices
    home:          withReset(homeReducer),
    articles:      withReset(articleReducer),
    category:      withReset(categoryReducer),
    topics:        withReset(topicsReducer),
    feeds:         withReset(feedsReducer),
    clips:         withReset(clipsReducer),
    search:        withReset(searchReducer),
    epaper:        withReset(epaperReducer),
    liveTv:        withReset(liveTvReducer),

    // Feature slices
    banner:        withReset(bannerReducer),
    sectionDetail: withReset(sectionDetailReducer),
    channel:       withReset(channelReducer),
    comments:      withReset(commentReducer),
    contentDetail: withReset(contentDetailReducer),
    city:          withReset(cityReducer),
    language:      withReset(languageReducer),
    subscribe:     withReset(subscribeReducer),
    social:        withReset(socialReducer),

    // Auth / User
    auth:          withReset(authReducer),
    profile:       withReset(profileReducer),
    navCategories: withReset(navCategoriesReducer),
  },
});

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