import { useModalsManagerContext } from "@/contexts/ModalsManagerContext";
import {
	type IorangePaymentStarter,
	getTransactionStatus,
	getTransactionsData,
} from "@/utils/requests/orangePayment";
import ErrorModal from "@components/modals/ErrorModal";
import InformationModal from "@components/modals/InformationModal";
import LoadingModal from "@components/modals/LoadingModal";
import { LOG } from "@logger";

import { useMutation, useQueryClient } from "@tanstack/react-query";
import * as WebBrowser from "expo-web-browser";
import { useState } from "react";

const log = LOG.extend("useOrangeMoney");

const paymentObjectDefault: IorangePaymentStarter = {
	// biome-ignore lint/style/useNamingConvention: <api expect type_paiement>
	type_paiement: 1,
	marchand: "1",
	service: "1",
	montant: 0,
	numero: "0707070707",
	commentaire: "Un commentaire",
};
const useOrangeMoney = () => {
	const queryClient = useQueryClient();

	const [isBrowserOpen, setIsBrowserOpen] = useState(false);
	const { showModal, closeModal } = useModalsManagerContext();

	const handlePaymentUsingBrowser = async (url: string) => {
		setIsBrowserOpen(true);
		const result = await WebBrowser.openBrowserAsync(url);
		// setResult(result);
		log.debug("handlePaymentUsingBrowser | Result ::", result);
		setIsBrowserOpen(false);
	};

	const orangeTransactionInitializerMutation = useMutation({
		mutationFn: (amount: number) =>
			getTransactionsData({
				...paymentObjectDefault,
				montant: amount,
			}),
		onSuccess: (data) => {
			// return data.payment_url
			log.debug("orangeTransactionInitializerMutation request success, opening browser...");
			queryClient.invalidateQueries({ queryKey: ["transactionsHistory"] });
			// await handlePaymentUsingBrowser(data.payment_url);
			// await transactionsStatusMutation.mutate(data.order_id);
			// setResult(result);
		},
		onError: (err) => {
			log.error("orangeTransactionInitializerMutation |", err);
		},
	});

	const maxRetry = 3;
	const retryDelay = 5000;

	const transactionsStatusMutation = useMutation({
		mutationFn: (orderId: string) => getTransactionStatus(orderId),
		onSuccess: (data) => {
			log.debug("transactionsStatusMutation request success");
			queryClient.invalidateQueries({ queryKey: ["transactionsHistory"] });
			return data.status;
		},
		onError: (err) => {
			log.error("transactionsStatusMutation |", err);
			queryClient.invalidateQueries({ queryKey: ["transactionsHistory"] });
		},
		// retry: (failureCount, error) => {
		// 	log.warn("transactionsStatusMutation | retrying", failureCount, error);
		// 	return failureCount < maxRetry;
		// },
		// retryDelay(_failureCount, _error) {
		// 	return retryDelay;
		// },
	});

	const openBrowserThenCheckStatus = async (paymentUrl: string, orderId: string) => {
		try {
			await handlePaymentUsingBrowser(paymentUrl);
			log.info("Verifying transaction status...");
			showModal(<LoadingModal message="Vérification du statut de la transaction..." />);
			await transactionsStatusMutation.mutateAsync(orderId);
		} catch (error) {
			log.error("handlePaymentButton |", error);
			if (error instanceof Error) {
				if (error.name === "ORANGE_PAYMENT_IN_PROGRESS") {
					log.warn("handlePaymentButton | ORANGE_PAYMENT_IN_PROGRESS");
					showModal(
						<InformationModal
							message="Le payment est toujours en cours."
							actionLabel="Rééssayer"
							onPress={async () =>
								await openBrowserThenCheckStatus(paymentUrl, orderId)
							}
						/>,
					);
				} else if (error.name === "ORANGE_PAYMENT_FAILED") {
					showModal(<ErrorModal message="Le paiment à échoué." />);
					log.error("handlePaymentButton | ORANGE_PAYMENT_FAILED");
				}
			} else {
				log.error("handlePaymentButton |", error);
				closeModal();
				throw error;
			}
		}
	};

	const orangePaymentTransactionHandler = async (amount: number) => {
		try {
			showModal(<LoadingModal message="Initialization de la transaction." />);
			const { payment_url, order_id } =
				await orangeTransactionInitializerMutation.mutateAsync(amount);
			await openBrowserThenCheckStatus(payment_url, order_id);
		} catch (error) {
			log.error("makePayment |", error);
		}
	};

	return {
		orangeTransactionInitializerMutation: orangeTransactionInitializerMutation,

		handlePaymentUsingBrowser,
		isBrowserOpen,
		isWaitingForOmPaymentUrl: orangeTransactionInitializerMutation.isPending,
		isCheckingForTransactionStatus: transactionsStatusMutation.isPending,
		transactionsStatusMutation,
		orangePaymentTransactionHandler,
	};
};

export default useOrangeMoney;
