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

/**
 * GET get_carcasses_buyer_info
 * @param {Object} payload
 * @param {string} payload.info - National code or legal ID
 * @param {string} payload.type - "person" | "unit"
 */
export const getCarcassesBuyerInfoService = createAsyncThunk(
  "GET_CARCASSES_BUYER_INFO_SERVICE",
  async (payload, { dispatch }) => {
    dispatch(LOADING_START());
    try {
      const { data, status } = await axios.get(
        `get_carcasses_buyer_info/?info=${encodeURIComponent(
          payload.info
        )}&type=${payload.type}`,
        { timeout: 30000 }
      );
      dispatch(LOADING_END());
      return { data, status };
    } catch (e) {
      dispatch(LOADING_END());
      return {
        error: e.response?.data?.result || "خطا در دریافت اطلاعات",
      };
    }
  }
);
