import axios from 'axios';
import { CBDCAxiosError } from '@/shared/@types/error';
import { resultify } from '@/shared/lib/resultify';
import { API_URL_ADDITIONAL_SERVICE } from '@/shared/api/api-url-additional-service';
import {
    DetailResponse,
    TitleInfo,
    PaymentInfo,
    ExtensionLinkPayWaitDetailParams,
    ExtensionLinkPayWaitDetailResponse
} from '../../model/types';
import {
    useMutation,
    UseMutationOptions
} from '@tanstack/react-query';

export const extensionLinkPayWaitDetail = async (params: ExtensionLinkPayWaitDetailParams): Promise<DetailResponse> => {
    const response = await resultify(
        axios.post<ExtensionLinkPayWaitDetailResponse>(API_URL_ADDITIONAL_SERVICE.extensionLinkPaymentWaitDetail(), params),
    );

    const detailResponse: DetailResponse = {
        titleInfo: {
            amount: response.amount,
            corpName: response.corpName,
            scheduledSendDate: response.scheduledSendDate
        } as TitleInfo,

        paymentInfo: {
            processStatus: response.processStatus,
            requestDate: response.requestDate,
            paymentLimitDate: response.paymentLimitDate,
            sendMethod: response.sendMethod,
            buyerName: response.buyerName,
            email: response.email,
            phoneNumber: response.phoneNumber,
            goodsName: response.goodsName,
            moid: response.moid
        } as PaymentInfo
    }

    return detailResponse
}

export const useExtensionLinkPayWaitDetailMutation = (options?: UseMutationOptions<DetailResponse, CBDCAxiosError, ExtensionLinkPayWaitDetailParams>) => {
    const mutation = useMutation<DetailResponse, CBDCAxiosError, ExtensionLinkPayWaitDetailParams>({
        ...options,
        mutationFn: (params: ExtensionLinkPayWaitDetailParams) => extensionLinkPayWaitDetail(params),
    });
    return {
        ...mutation,
    };
};