// import { createSlice, PayloadAction } from '@reduxjs/toolkit';

// // Define the item type
// interface CartItem {
//   id: string;
//   name: string;
//   price: number;
//   quantity: number;
//   image :string ;
// }

// // Define the initial state
// interface CartState {
//   items: CartItem[];
// }

// const initialState: CartState = {
//   items: [],
// };

// // Create the slice
// const cartSlice = createSlice({
//   name: 'cart',
//   initialState,
//   reducers: {
//     addItem(state, action: PayloadAction<CartItem>) {
//       const newItem = action.payload;
//       const existingItem = state.items.find(item => item.id === newItem.id);

//       if (existingItem) {
//         existingItem.quantity += newItem.quantity;
//       } else {
//         state.items.push(newItem);
//       }
//     },
//     removeItem(state, action: PayloadAction<string>) {
//       const itemIdToRemove = action.payload;
//       state.items = state.items.filter(item => item.id !== itemIdToRemove);
//     },
//     updateQuantity(state, action: PayloadAction<{ id: string; quantity: number }>) {
//       const { id, quantity } = action.payload;
//       const itemToUpdate = state.items.find(item => item.id === id);

//       if (itemToUpdate) {
//         itemToUpdate.quantity = quantity;
//       }
//     },
//   },
// });

// // Export the actions
// export const { addItem, removeItem, updateQuantity } = cartSlice.actions;

// // Export the reducer
// export default cartSlice.reducer;


// UPDATED SLICE FOR API CALLS

// import { createSlice, PayloadAction } from '@reduxjs/toolkit';

// // Define the item type
// interface CartItem {
//   productId: string;
//   // name: string;
//   amount: string;
//   quantity: string;
//   // image :string ;
//   userId:string
// }

// // Define the initial state
// interface CartState {
//   items: CartItem[];
// }

// const initialState: CartState = {
//   items: [],
// };

// // Create the slice
// const cartSlice = createSlice({
//   name: 'cart',
//   initialState,
//   reducers: {
//     addItem(state, action: PayloadAction<CartItem>) {
//       const newItem = action.payload;
//       const existingItem = state.items.find(item => item.productId === newItem.productId);

//       if (existingItem) {
//         existingItem.quantity += newItem.quantity;
//       } else {
//         state.items.push(newItem);
//       }
//     },
//     removeItem(state, action: PayloadAction<string>) {
//       const itemIdToRemove = action.payload;
//       state.items = state.items.filter(item => item.productId !== itemIdToRemove);
//     },
//     updateQuantity(state, action: PayloadAction<{ id: string; quantity: string }>) {
//       const { id, quantity } = action.payload;
//       const itemToUpdate = state.items.find(item => item.productId === id);

//       if (itemToUpdate) {
//         itemToUpdate.quantity = quantity;
//       }
//     },
//     addItems(state, action: PayloadAction<CartItem[]>) {
//       state.items.push(...action.payload);
//     },
//   },
// });

// // Export the actions
// export const { addItem, removeItem, updateQuantity, addItems } = cartSlice.actions;

// // Export the reducer
// export default cartSlice.reducer;


// THUNK 

import { createSlice, PayloadAction, createAsyncThunk } from '@reduxjs/toolkit';
import { fetchInitialCartData } from './cartAsyncActions';

// Define the item type
interface CartItem {
  _id:string;
  productId: string;
  product:{
    categoryId:string,
    subCategoryId:string
  };
  amount: string;
  quantity: string;
  userId: string;
}

// Define the initial state
interface CartState {
  items: CartItem[];
}

const initialState: CartState = {
  items: [],
};

// Create the slice
const cartSlice = createSlice({
  name: 'cart',
  initialState,
  reducers: {
    addItem(state, action: PayloadAction<CartItem>) {
      const newItem = action.payload;
      const existingItem = state.items.find(item => item.productId === newItem.productId);

      if (existingItem) {
        existingItem.quantity += newItem.quantity;
      } else {
        state.items.push(newItem);
      }
    },
    removeItem(state, action: PayloadAction<string>) {
      const itemIdToRemove = action.payload;
      state.items = state.items.filter(item => item._id !== itemIdToRemove);
    },
    updateQuantity(state, action: PayloadAction<{ id: string; quantity: string }>) {
      const { id, quantity } = action.payload;
      const itemToUpdate = state.items.find(item => item.productId === id);

      if (itemToUpdate) {
        itemToUpdate.quantity = quantity;
      }
    },
    addItems(state, action: PayloadAction<CartItem[]>) {
      state.items.push(...action.payload);
    },
  },
  extraReducers: builder => {
    builder.addCase(fetchInitialCartData.fulfilled, (state, action) => {
      state.items = action.payload;
    });
  },
});

export const { addItem, removeItem, updateQuantity, addItems } = cartSlice.actions;

// Export the reducer
export default cartSlice.reducer;