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, (categories_status, action) => {
      pizza_status.categories_status = Status.LOADING;
      categories_status.categories_items = [];
    });

    builder.addCase(fetchCategories.fulfilled, (categories_status, action) => {
      categories_status.categories_status = Status.SUCCESS;
      categories_status.categories_items = action.payload;
    });

    builder.addCase(fetchCategories.rejected, (categories_status, action) => {
      categories_status.categories_status = Status.ERROR;
      categories_status.categories_items = [];
    });
  },
});

export const { setCategories } = categoriesSlice.actions;

export default categoriesSlice.reducer;
