import queryGenerator from "@/utils/queryGenarator";
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import axios from "axios";
import { errorHandler, successHandler } from "../../../../utils/functions";

const initialState = {
  info: null,
  error: "",
  loading: false,
};

export const loadDashboardData = createAsyncThunk(
  "dashboard/loadDashboardData",
  async (q) => {
    try {
      const query = queryGenerator(q);
      const { data } = await axios.get(`dashboard?${query}`);
      return successHandler(data);
    } catch (error) {
      return errorHandler(error);
    }
  }
);

const dashboardSlice = createSlice({
  name: "dashboard",
  initialState,
  reducers: {
    clearDashboard: (state) => {
      state.info = null;
    },
  },

  extraReducers: (builder) => {
    // 1) ====== builders for loadDashboardData ======

    builder.addCase(loadDashboardData.pending, (state) => {
      state.loading = true;
    });

    builder.addCase(loadDashboardData.fulfilled, (state, action) => {
      state.loading = false;
      state.info = action.payload?.data;
    });
  },
});

export default dashboardSlice.reducer;
export const { clearDashboard } = dashboardSlice.actions;
