/**
 * File: master.thunk.ts
 * Purpose: This file contains asynchronous thunk actions for managing master-related workflows 
 *          such as master, and more.
 *          
 * Description:
 * - Utilizes `redux-thunk` to create async actions that handle API requests and responses.
 * - Handles master API calls by dispatching appropriate Redux actions based on the API response.
 * - Displays error notifications using the `Toast` component in case of API failures.
*/

import apiClient from "@/src/interceptor/apiClient";
import { masterActions } from "../../slices/Master/master";
import { Toast } from "@/src/components/Toast/index";
import { API_ENDPOINTS } from "@/src/utils/commonVariables";


/**
 * Thunk action for getting master country list.
 * @param payload - The payload containing the master country list details.
 * @returns A thunk function that dispatches actions based on the API response.
 */
export const getMasterCountryList = (payload: any) => {
    return async (dispatch: any) => {
        dispatch(masterActions.RequestMasterCountryList());
        try {
            const response = await apiClient.post(API_ENDPOINTS.LIST_COUNTRY, payload);
            dispatch(masterActions.SuccessMasterCountryList(response.data));
            // Toast('success', response.data.meta.message);
        } catch (error) {
            dispatch(masterActions.ErrorMasterCountryList());
            Toast('error', error.response.data.message);
        }
    }
}

/** Asynchronous thunk action for media upload process.
 * @param payload - The data required for media upload.
 * @returns A thunk function that dispatches actions based on API response.
 */
export const mediaUpload = (payload: any, callback?: (data: any) => void) => async (dispatch: any) => {
    try {
        dispatch(masterActions.requestMediaUpload());
        const { data } = await apiClient.post(API_ENDPOINTS.MEDIA_UPLOAD, payload);
        callback && callback(data);
        dispatch(masterActions.responseMediaUpload(data?.data));
    } catch (error: any) {
        dispatch(masterActions.errorMediaUpload());
        Toast("error", error?.message)
    }
}

/**
 * Asynchronous thunk action for media delete process.
 * @param payload - The data required for media delete.
 * @returns A thunk function that dispatches actions based on API response.
 */
export const mediaDelete = (payload: any, callback?: (data: any) => void) => async (dispatch: any) => {
    try {
        dispatch(masterActions.requestMediaDelete());
        const { data } = await apiClient.post(API_ENDPOINTS.DELETE_MEDIA, payload);
        callback && callback(data);
        dispatch(masterActions.responseMediaDelete(data?.data));
    } catch (error: any) {
        dispatch(masterActions.errorMediaDelete());
        Toast("error", error?.message)
    }
}

