import axios from '../Config/AxiosConfig'
import type { Dispatch } from '@reduxjs/toolkit'
import { ErrorMessage } from '../Utils/Notification'
import { setLoadingStatus } from '../Reducer/Common'
import { overview } from '../Reducer/DashboardReducer'
import { overviewDetail } from '../Reducer/ClientOverviewReducer'

export const GetClientOverview = async (dispatch: Dispatch): Promise<void> => {
    dispatch(setLoadingStatus({ status: true }))

    await axios
        .get(`api/Overview/GetClientOverview`)
        .then((response) => {
            if (response.status === 200) {
                dispatch(overview(response.data.data))
                dispatch(setLoadingStatus({ status: false }))
            } else {
                dispatch(setLoadingStatus({ status: false }))
                ErrorMessage(response.data.error.message)
            }
        })
        .catch(() => {
            dispatch(setLoadingStatus({ status: false }))
        })
        .finally(() => {
            dispatch(setLoadingStatus({ status: false }))
        })
}

export const GetClientOverviewDetail = async (
    dispatch: Dispatch,
    sequenceNo: number,
    clientCategoryId: number,
    pageNo: number,
    pageSize: number,
    search: string,
    token: string
): Promise<void> => {
    dispatch(setLoadingStatus({ status: true }))
    await axios
        .get(
            `api/Overview/GetClientOverviewDetail/${sequenceNo}/${clientCategoryId}/${pageNo}/${pageSize}/${search}?accessToken=${token}`
        )
        .then((response) => {
            if (response.status === 200) {
                dispatch(overviewDetail(response.data.data))
                dispatch(setLoadingStatus({ status: false }))
            } else {
                dispatch(setLoadingStatus({ status: false }))
                ErrorMessage(response.data.error.message)
            }
        })
        .catch(() => {
            dispatch(setLoadingStatus({ status: false }))
        })
        .finally(() => {
            dispatch(setLoadingStatus({ status: false }))
        })
}
