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

export const extensionLinkPayHistoryDetail = async (params: ExtensionLinkPayHistoryDetailParams): Promise<DetailResponse> => {
    const response = await resultify(
        axios.post<ExtensionLinkPayHistoryDetailResponse>(API_URL_ADDITIONAL_SERVICE.extensionLinkPaymentHistoryDetail(), params),
    );

    const detailResponse: DetailResponse = {
        titleInfo: {
            amount: response.amount,
            corpName: response.corpName,
            sendDate: response.sendDate
        } as TitleInfo,
        paymentInfo: {
            buyerName: response.buyerName,
            sendMethod: response.sendMethod,
            sendDate: response.sendDate,
            paymentStatus: response.paymentMethod,
            failCount: response.failCount,
            paymentMethod: response.paymentMethod,
            paymentDate: response.paymentDate,
            paymentLimitDate: response.paymentLimitDate
        } as PaymentInfo,
        detailInfo: {
            email: response.email,
            phoneNumber: response.phoneNumber,
            goodsName: response.goodsName,
            moid: response.moid
        } as DetailInfo
    }

    return detailResponse
}

export const useExtensionLinkPayHistoryDetailMutation = (options?: UseMutationOptions<DetailResponse, CBDCAxiosError, ExtensionLinkPayHistoryDetailParams>) => {
        const mutation = useMutation<DetailResponse, CBDCAxiosError, ExtensionLinkPayHistoryDetailParams>({
            ...options,
            mutationFn: (params: ExtensionLinkPayHistoryDetailParams) => extensionLinkPayHistoryDetail(params),
        });
        return {
            ...mutation,
        };
}