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

export const provincePolicyGetAccountsSevice = createAsyncThunk(
  "PROVINCE_POLICY_GET_ACCOUNTS_SERVICE",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.get("zarinpal-accounts/");
    dispatch(LOADING_END());
    return { data, status };
  }
);

export const provincePolicyEditAccountsSevice = createAsyncThunk(
  "PROVINCE_POLICY_EDIT_ACCOUNTS_SERVICE",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.put("zarinpal-accounts/0/", d);
    dispatch(LOADING_END());
    return { data, status };
  }
);

export const provincePolicyGetBeneficiaryAccountsSevice = createAsyncThunk(
  "PROVINCE_POLICY_GET_BENEFICIARY_ACCOUNTS_SERVICE",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.get("beneficiary-accounts/");
    dispatch(LOADING_END());
    return { data, status };
  }
);

export const provincePolicySubmitBeneficiaryAccountsSevice = createAsyncThunk(
  "PROVINCE_POLICY_SUBMIT_BENEFICIARY_ACCOUNTS_SERVICE",
  async (d, { dispatch }) => {
    try {
      dispatch(LOADING_START());
      const { data, status } = await axios.post("beneficiary-accounts/", d);
      dispatch(LOADING_END());
      return { data, status };
    } catch (e) {
      dispatch(LOADING_END());
      return { error: e.response.data.result };
    }
  }
);

export const provincePolicyEditBeneficiaryAccountsSevice = createAsyncThunk(
  "PROVINCE_POLICY_EDIT_BENEFICIARY_ACCOUNTS_SERVICE",
  async (d, { dispatch }) => {
    try {
      dispatch(LOADING_START());
      const { data, status } = await axios.put("beneficiary-accounts/0/", d);
      dispatch(LOADING_END());
      return { data, status };
    } catch (e) {
      dispatch(LOADING_END());
      return { error: e.response.data.result };
    }
  }
);

export const provincePolicyDeleteBeneficiaryAccountsSevice = createAsyncThunk(
  "PROVINCE_POLICY_DELETE_BENEFICIARY_ACCOUNTS_SERVICE",
  async (id, { dispatch }) => {
    dispatch(LOADING_START());
    try {
      const { data, status } = await axios.delete(
        "beneficiary-accounts/0/?account_key=" + id
      );
      dispatch(LOADING_END());
      return { data, status };
    } catch (e) {
      dispatch(LOADING_END());
      return { error: e.response.data.result };
    }
  }
);
