/**
 * @author: Multiqos
 * File: propertyThunks.ts
 * Purpose: This file contains asynchronous thunk actions for managing property-related workflows 
 *          such as property list, and more.
 *          
 * Description:
 * - Utilizes `redux-thunk` to create async actions that handle API requests and responses.
 * - Handles property 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 { API_ENDPOINTS } from "@/src/utils/commonVariables";
import { propertyActions } from "../../slices/Property/property";
import { Toast } from "@/src/components/Toast";

/**
 * Asynchronous thunk action for property list process.
 * @param payload - The data required for property list (e.g., { page, perPage }).
 * @returns A thunk function that dispatches actions based on API response.
 */
export const getPropertyList =
    (payload: any) =>
        async (dispatch: any) => {
            try {
                if (payload.countOnly) {
                    dispatch(propertyActions.requestFilterPropertyList());
                } else {
                    dispatch(propertyActions.requestPropertyList());
                }
                const response: any = await apiClient.post(API_ENDPOINTS.LIST_PROPERTY, payload);
                if (payload.countOnly) {
                    dispatch(
                        propertyActions.successFilterPropertyList({
                            meta: response?.data?.meta,
                        })
                    );
                } else {
                    dispatch(
                        propertyActions.successPropertyList({
                            data: response?.data?.data,
                            meta: response?.data?.meta,
                            page: payload.page,
                        })
                    );
                }
            } catch (error: any) {
                if (payload.countOnly) {
                    dispatch(propertyActions.failureFilterPropertyList());
                } else {
                    dispatch(propertyActions.failurePropertyList());
                }
                Toast("error", error?.response?.data?.message);
            }
        };

export default getPropertyList;

/**
 * Asynchronous thunk action for location list process.
 * @param payload The data required for location list
 * @returns A thunk function that dispatches actions based on API response.
 */
export const getLocationList =
    (payload: any) =>
        async (dispatch: any) => {
            try {
                dispatch(propertyActions.requestLocationList());
                const response: any = await apiClient.post(API_ENDPOINTS.LIST_LOCATION, payload);
                dispatch(
                    propertyActions.successLocationList(response?.data)
                );
            } catch (error: any) {
                dispatch(propertyActions.failureLocationList());
                Toast("error", error?.response?.data?.message);
            }
        };


/**
 * Asynchronous thunk action for filters list process.
 * @param payload The data required for filters list
 * @returns A thunk function that dispatches actions based on API response.
 */
export const getFiltersList =
    () =>
        async (dispatch: any) => {
            try {
                dispatch(propertyActions.requestFiltersList());
                const { data }: any = await apiClient.post(API_ENDPOINTS.LIST_FILTERS);
                dispatch(
                    propertyActions.successFiltersList(data?.data)
                );
            } catch (error: any) {
                dispatch(propertyActions.failureFiltersList());
                Toast("error", error?.response?.data?.message);
            }
        };

