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

export const slaughterGetPriceService = createAsyncThunk(
  "SLAUGHTER_GET_PRICE_SERVICE",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.get("broadcast-price/", {
      params: { ...d },
    });
    dispatch(LOADING_END());
    return { data, status };
  }
);

export const slaughterGetGuildsService = createAsyncThunk(
  "SLAUGHTER_GET_GUILDS_FOR_COMMONLY_USED",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());

    const { data, status } = await axios.get("guilds/", {
      params: {
        role: getRoleFromUrl(),
        commonly_used: true,
        ...d,
      },
    });

    dispatch(LOADING_END());
    return { data, status };
  }
);

export const slaughterAddDailyListService = createAsyncThunk(
  "SLAUGHTER_ADD_DAILY_LIST_SERVICE",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.post("commonly-used/", {
      role: getRoleFromUrl(),
      guild_key_list: d.guild_key_list,
      ...d,
    });
    dispatch(LOADING_END());
    return { data, status };
  }
);
export const submitBatchAllocationsService = createAsyncThunk(
  "SUBMIT_BATCH_ALLOCATIONS",
  async (allocations, { dispatch }) => {
    dispatch(LOADING_START());
    try {
      const response = await axios.post("batch-steward-allocation/", {
        allocations_list: allocations,
      });
      return response.data;
    } finally {
      dispatch(LOADING_END());
    }
  }
);

export const slaughterDeleteDailyListService = createAsyncThunk(
  "SLAUGHTER_DELETE_DAILY_LIST_SERVICE",
  async (commonly_used_key, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.delete(
      `commonly-used/0/?commonly_used_key=${commonly_used_key}`
    );
    dispatch(LOADING_END());
    return { data, status };
  }
);
