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

export const extensionAccountHolderSearchDetail = async (params: ExtensionAccountHolderSearchDetailParams): Promise<DetailResponse> => {
    const response = await resultify(
        axios.post<ExtensionAccountHolderSearchDetailResponse>(API_URL_ADDITIONAL_SERVICE.extensionAccountHolderSearchDetail(), params),
    );

    // ExtensionAccountHolderSearchDetailResponse를 DetailResponse로 변환
    const detailResponse: DetailResponse = {
        titleInfo: {
            accountNo: response.accountNo,
            bankName: response.bankName,
            requestDate: response.requestDate
        } as TitleInfo,
        detailInfo: {
            accountName: response.accountName,
            requestDate: response.requestDate,
            resultStatus: response.resultStatus,
            failureReason: response.failReason,
            bankName: response.bankName,
            accountNo: response.accountNo,
            requestWay: response.requestWay
        } as DetailInfo

    };

    return detailResponse;
};

export const useExtensionAccountHolderSearchDetailMutation = (options?: UseMutationOptions<DetailResponse, CBDCAxiosError, ExtensionAccountHolderSearchDetailParams>) => {
    const mutation = useMutation<DetailResponse, CBDCAxiosError, ExtensionAccountHolderSearchDetailParams>({
        ...options,
        mutationFn: (params: ExtensionAccountHolderSearchDetailParams) => extensionAccountHolderSearchDetail(params),
    });
    return {
        ...mutation,
    };
}