import { createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
import { LOADING_END, LOADING_START } from "../../../lib/redux/slices/appSlice";
import { getRoleFromUrl } from "../../../utils/getRoleFromUrl";

export const fetchStewardBroadcastAndProducts = createAsyncThunk(
  "STEWARD_FETCH_BROADCAST_AND_PRODUCTS",
  async (d, { dispatch }) => {
    try {
      dispatch(LOADING_START());
      const [broadcastResponse, productsResponse] = await Promise.all([
        axios.get("steward-sales-info-dashboard/", {
          params: {
            role: getRoleFromUrl(),
            role_key: d.role_key || "",
          },
        }),
        axios.get("roles-products", {
          params: {
            role: getRoleFromUrl(),
            role_key: d.role_key || "",
          },
        }),
      ]);

      dispatch(LOADING_END());

      return {
        broadcastData: broadcastResponse.data,
        productsData: productsResponse.data,
      };
    } catch (error) {
      dispatch(LOADING_END());
      throw error;
    }
  }
);
