import axios from 'axios';
import { NiceAxiosError } 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
} from '../../model/types';
import {
    useMutation,
    UseMutationOptions
} from '@tanstack/react-query';
import { ExtensionLinkPayWaitDetailParams, ExtensionLinkPayWaitDetailResponse } from '../../model/link-pay/types';
import { getHeaderUserAgent } from '@/shared/constants/url';

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

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