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,
    DetailInfo,
    ExtensionAccountHolderAuthDetailParams,
    ExtensionAccountHolderAuthDetailResponse
} from '../../model/types';
import {
    useMutation,
    UseMutationOptions
} from '@tanstack/react-query';

export const extensionAccountHolderAuthDetail = async (params: ExtensionAccountHolderAuthDetailParams): Promise<DetailResponse> => {
    const response = await resultify(
        axios.post<ExtensionAccountHolderAuthDetailResponse>(API_URL_ADDITIONAL_SERVICE.extensionAccountHolderAuthDetail(), params),
    );

    const detailResponse: DetailResponse = {
        titleInfo: {
            accountName: response.accountName,
            accountNo: response.accountNo,
            scheduledSendDate: response.requestDate //추후 발송예정일자로 수정
        } as TitleInfo,
        detailInfo: {
            companyName: response.companyName,
            mid: response.mid,
            requestDate: response.requestDate,
            bankName: response.bankName,
            accountNo: response.accountNo,
            accountName: response.accountName,
            transferStatus: response.transferStatus,
            failureReason: response.failReason,
        } as DetailInfo

    };

    return detailResponse;
};

export const useExtensionAccountHolderAuthDetailMutation = (options?: UseMutationOptions<DetailResponse, CBDCAxiosError, ExtensionAccountHolderAuthDetailParams>) => {
    const mutation = useMutation<DetailResponse, CBDCAxiosError, ExtensionAccountHolderAuthDetailParams>({
        ...options,
        mutationFn: (params: ExtensionAccountHolderAuthDetailParams) => extensionAccountHolderAuthDetail(params),
    });
    return {
        ...mutation,
    };
}