import type { Dispatch } from '@reduxjs/toolkit'
import axios from '../Config/AxiosConfig'

import { setLoadingStatus } from '../Reducer/Common'

import { ErrorMessage, SuccessMessage } from '../Utils/Notification'
import {
    type bulkmailFormDataInterface,
    type savebulksms,
} from '../Types/Bulksms'
import { contactdepartmentDropDownList } from '../Reducer/InitialReducer'

export const GetContactDetail = (
    dispatch: Dispatch,
    clientCategoryId: number,
    flagIds: number[],
    statusId: number
): void => {
    dispatch(setLoadingStatus({ status: true }))
    const flagIdParam = flagIds.join(',')
    axios
        .get(
            `/api/Management/GetContactDetail/${clientCategoryId}/${flagIdParam}/${statusId}`
        )
        .then((response) => {
            if (response.status === 200) {
                const newData = response?.data?.data?.map(
                    (value: bulkmailFormDataInterface) => {
                        return {
                            ...value,
                            key: value.contactName,
                        }
                    }
                )
                response.data.data = newData
                dispatch(contactdepartmentDropDownList({ ...response.data }))
            } else {
                ErrorMessage(response.data.operationMsg)
            }
            dispatch(setLoadingStatus({ status: false }))
        })
        .catch((error) => {
            console.error('Error fetching posts:', error)
            dispatch(setLoadingStatus({ status: false }))
        })
}
export const SaveBulkSms = async (
    dispatch: Dispatch,
    data: savebulksms
): Promise<any> => {
    dispatch(setLoadingStatus({ status: true }))
    return await axios
        .post(`/api/Management/SetClientSMS`, data)
        .then((response) => {
            dispatch(setLoadingStatus({ status: false }))
            if (response.status === 200) {
                SuccessMessage(response.data.operationMsg)
                return true
            } else {
                ErrorMessage(response.data.operationMsg)
                return false
            }
        })
        .catch((error) => {
            console.error('Error fetching posts:', error)
            dispatch(setLoadingStatus({ status: false }))
            return false
        })
}
