// src/store/slices/userSlice.js
import { createSlice } from "@reduxjs/toolkit";
import { userSliceReducers } from "../reducers/userSliceReducers";
import { getUserData } from "../thunks/userThunk";

/**
 * Initial shape of User State
 */
const initialUserState = {
  user: {
    name: "Ram",
    avatar: "R",
  },
};

/**
 * User Slice: manages profile data, preferences, etc.
 */
const userSlice = createSlice({
  name: "user",
  initialState: initialUserState,
  reducers: userSliceReducers,

  extraReducers: (builder) => {
    builder.addCase(getUserData.fulfilled, (state, action) => {
      state.user = action.payload;
    });
  },
});

/**
 * Selector: Get user object from store
 */
export const selectUser = (state) => state.user.user;

/**
 * Export reducers (actions) from userSliceReducers
 */
export const { changeUserData } = userSlice.actions;

/**
 * Export slice reducer
 */
export default userSlice.reducer;
