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

export const extensionLinkPayHistoryDetail = async (params: ExtensionLinkPayHistoryDetailParams): Promise<DetailResponse> => {
    let headerOptions = {
        menuId: 54,
        apiType: 'SELECT'
    };
    let options = {
        headers: {
            'X-User-Agent': getHeaderUserAgent(headerOptions)
        }
    };
    const response = await resultify(
        axios.post<ExtensionLinkPayHistoryDetailResponse>(API_URL_ADDITIONAL_SERVICE.extensionLinkPaymentHistoryDetail(), params, options),
    );

    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.paymentStatus,
            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, NiceAxiosError, ExtensionLinkPayHistoryDetailParams>) => {
        const mutation = useMutation<DetailResponse, NiceAxiosError, ExtensionLinkPayHistoryDetailParams>({
            ...options,
            mutationFn: (params: ExtensionLinkPayHistoryDetailParams) => extensionLinkPayHistoryDetail(params),
        });
        return {
            ...mutation,
        };
}