import { LOG } from "@logger";
import base64 from "react-native-base64";
import axiosRequest from "../axiosRequest";

export interface IorangePaymentStarter {
	// biome-ignore lint/style/useNamingConvention: <api expect type_paiement>
	type_paiement: number;
	marchand: "1";
	service: string;
	montant: number;
	commentaire: string;
	numero: string;
}

export interface IorangeResponse {
	status: number;
	message: string;
	// biome-ignore lint/style/useNamingConvention: <api expect pay_token>
	pay_token: string;
	// biome-ignore lint/style/useNamingConvention: <api expect payment_url>
	payment_url: string;
	// biome-ignore lint/style/useNamingConvention: <api expect notif_token>
	notif_token: string;
	// biome-ignore lint/style/useNamingConvention: <api expect order_id>
	order_id: string;
}

const basictoken = base64.encode("admin:admin");

export type OrangeStatus = "INITIATED" | "SUCCESS" | "FAILED";
export interface IorangePaymentStatus {
	status: OrangeStatus;
	code: number;
	message: {
		status: OrangeStatus;
		// biome-ignore lint/style/useNamingConvention: <api expect order_id>
		order_id: string;
		txnid?: string;
	};
}

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

export const getTransactionsData = async (payload: IorangePaymentStarter) => {
	log.http("getTransactionsData", payload);

	// const basictoken = base64.encode("admin:admin");
	const response = await axiosRequest<IorangeResponse>({
		url: "/transactions/",
		method: "POST",
		headers: {
			// biome-ignore lint/style/useNamingConvention: <explanation>
			Authorization: `Basic ${basictoken}`,
		},
		data: payload,
	});

	log.http("getTransactionsData |", JSON.stringify(response, null, 2));

	return response;
};

export const getTransactionStatus = async (orderId: string) => {
	log.http("getTransactionStatus |", { orderId });

	try {
		const response = await axiosRequest<IorangePaymentStatus>({
			url: `/api/TransactionCheckStatus/${orderId}/`,
			method: "GET",
			headers: {
				// biome-ignore lint/style/useNamingConvention: <explanation>
				Authorization: `Basic ${basictoken}`,
			},
		});

		log.http("getTransactionStatus |", JSON.stringify(response, null, 2));
		switch (response.status) {
			case "SUCCESS": {
				log.http("getTransactionStatus |", JSON.stringify(response, null, 2));
				return response;
			}
			case "INITIATED": {
				log.warn("Payment is still in progress, throwing error for mutation to catch");
				const error = new Error("Payment is still in progress");
				error.name = "ORANGE_PAYMENT_IN_PROGRESS";
				throw error;
			}
			case "FAILED": {
				log.warn("Payment failed, throwing error for mutation to catch");
				const error = new Error("Payment failed");
				error.name = "ORANGE_PAYMENT_FAILED";
				throw error;
			}

			default: {
				log.warn("An unknown error occured, throwing error for mutation to catch");
				const error = new Error("Payment failed");
				error.name = "ORANGE_UNKNOWN_PAYMENT_ERROR";
				throw error;
			}
		}
	} catch (error) {
		log.error(
			"getTransactionStatus | An unexpected error occured |",
			JSON.stringify(error, null, 2),
		);
		throw error;
	}
};
