// store/sageBuilder/slice.ts
import { createSlice } from "@reduxjs/toolkit";
import { SageBuilderState } from "./type";
import { getSageBuilderDashboard } from "./thunk";

const initialState: SageBuilderState = {
    status: "idle",
    error: null,
    htmlContent: null,
};

const sageBuilderSlice = createSlice({
  name: "sageBuilder",
  initialState,
  reducers: {
    resetSageBuilder: (state) => {
      state.status = "idle";
      state.error = null;
      state.htmlContent = null;
    }
  },
  extraReducers: (builder) => {
    /**
     * Get Sage Builder Dashboard
     */
    builder.addCase(getSageBuilderDashboard.fulfilled, (state, action) => {
      state.htmlContent = action.payload;
      state.error = null;
      state.status = "succeeded";
    });

    builder.addCase(getSageBuilderDashboard.pending, (state) => {
      state.status = "loading";
      state.error = null;
    });

    builder.addCase(getSageBuilderDashboard.rejected, (state, action) => {
      state.status = "failed";
      state.error = action.error.message || "Une erreur est survenue";
    });
  },
});

export const { resetSageBuilder } = sageBuilderSlice.actions;
export default sageBuilderSlice.reducer;