import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { fetchCategories } from './stock';
import { Categories, CategoriesSliceState, Status } from './types';

const initialState: PizzaSliceState = {
    pizza_items: [],
    pizza_status: Status.LOADING, // loading | success | error
};

const pizzaSlice = createSlice({
  name: 'pizza',
  initialState,
  reducers: {
    setPizza(pizza_status, action: PayloadAction<Pizza[]>) {
      pizza_status.pizza_items = action.payload;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(fetchCategories.pending, (pizza_status, action) => {
      pizza_status.pizza_status = Status.LOADING;
      pizza_status.pizza_items = [];
    });

    builder.addCase(fetchPizza.fulfilled, (pizza_status, action) => {
      pizza_status.pizza_status = Status.SUCCESS;
      pizza_status.pizza_items = action.payload;
    });

    builder.addCase(fetchPizzas.rejected, (pizza_status, action) => {
      pizza_status.pizza_status = Status.ERROR;
      pizza_status.pizza_items = [];
    });
  },
});

export const { setPizza } = pizzaSlice.actions;

export default pizzaSlice.reducer;
