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

export const provinceSendReportGetTimes = createAsyncThunk(
  "PROVINCE_SEND_REPORT_GET_TIMES",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.get("report-submission/");
    dispatch(LOADING_END());
    return { data, status };
  }
);

export const provinceSendReportGetPositions = createAsyncThunk(
  "PROVINCE_SEND_REPORT_GET_POSITIONS",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.get("user-position/");
    dispatch(LOADING_END());
    return { data, status };
  }
);

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

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

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

export const provinceSendReportGetReports = createAsyncThunk(
  "PROVINCE_SEND_REPORT_GET_REPORTS",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.get("reports/");
    dispatch(LOADING_END());
    return { data, status };
  }
);
