// store/ask-ia/slice.ts
import { createSlice } from "@reduxjs/toolkit";
import { AskiaState } from "./type";
import { getAskIa } from "./thunk";

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

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

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

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

export const { resetAskIa } = askIaSlice.actions;
export default askIaSlice.reducer;