import { useApolloClient } from "@apollo/client/react";
import { 
    CREATE_PAYMENT_INITIATE,
    CREATE_PAYMENT_CALLBACK,
    CREATE_CANCEL_ORDER,
    CREATE_PAYMENT_REPLAY
} from "@/graphql";
import {
    CreatePaymentInitiateVariables, 
    CreatePaymentCallbackVariables,
    CreateCancelOrderVariables,
    CreatePaymentInitiate,
    CreatePaymentCallback,
    CreateCancelOrder,
    CreatePaymentReplayVariables,
    CreatePaymentReplay
} from "@/types/checkout/type";

export interface CreateOrderResult {
    data: CreatePaymentInitiate | null;
    msg: string;
    error: boolean;
}
interface CreatePaymnetCallbackResult {
    data: CreatePaymentCallback | null;
    msg: string;
    error: boolean;
}
interface CreateCancelOrderResult {
    data: CreateCancelOrder | null;
    msg: string;
    error: boolean;
}
interface PaymentReplayResult {
    error: boolean;
    data: CreatePaymentReplay | null;
    msg: string;
}
export const usePlaceOrder = () => {
    const client = useApolloClient();

    const createOrder = (params: CreatePaymentInitiateVariables = {}) => {
        return client.mutate({
            mutation: CREATE_PAYMENT_INITIATE,
            variables: params
        }).then((res) => {
            const resData = res?.data?.createPaymentInitiate?.paymentInitiate ?? null; 
            const result: CreateOrderResult = { 
                data:resData,
                msg: resData?.message ?? '',
                error: false
            };
            return result;
        }).catch((err) => {
            const result: CreateOrderResult = {
                data: null,
                msg: err.message,
                error: true
            };
            return result;
        });

    };

    // 支付回调
    const createPaymentCallback = (params: CreatePaymentCallbackVariables) => { 
        return client.mutate({
            mutation: CREATE_PAYMENT_CALLBACK,
            variables: params
        }).then((res) => {
            const resData = res?.data?.createPaymentCallback?.paymentCallback ?? null; 
            const result: CreatePaymnetCallbackResult = { 
                data:resData,
                msg: resData?.message ?? '',
                error: false
            };
            return result;
        }).catch((err) => {
            const result: CreatePaymnetCallbackResult = {
                data: null,
                msg: err.message,
                error: true
            };
            return result;
        });
    };

    // 取消订单
    const cancelOrder = (params: CreateCancelOrderVariables) => { 
        return client.mutate({
            mutation: CREATE_CANCEL_ORDER,
            variables: params
        }).then((res) => {
            const resData = res?.data?.createCancelOrder?.cancelOrder ?? null; 
            const result: CreateCancelOrderResult = { 
                data:resData,
                msg: resData?.message ?? '',
                error: false
            };
            return result;
        }).catch((err) => {
            const result: CreateCancelOrderResult = {
                data: null,
                msg: err.message,
                error: true
            };
            return result;
        });
    }

    const paymentRepay = (params: CreatePaymentReplayVariables) => { 

        return client.mutate({
            mutation: CREATE_PAYMENT_REPLAY,
            variables: params
        }).then((res) => {
            const resData = res?.data?.createPaymentReplay?.paymentReplay ?? null; 
            const result: PaymentReplayResult = {
                error: false,
                msg: '',
                data: resData
            };
            return result;
        }).catch((err) => {
            const result: PaymentReplayResult = {
                error: true,
                msg: err.message,
                data: null,
            };
            return result;
        });
    };

  return {
    createOrder,
    createPaymentCallback,
    cancelOrder,
    paymentRepay
  };
};
