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

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

export interface IwavePaymentStarter {
	// biome-ignore lint/style/useNamingConvention: <API requirement>
	type_paiement: 2; // id 2 is for wave.
	marchand: string;
	service: string;
	montant: number;
}

export interface IwaveStarterRespone {
	id: string;
	amount: string;
	// biome-ignore lint/style/useNamingConvention: <api response>
	checkout_status: string;
	// biome-ignore lint/style/useNamingConvention: <api response>
	client_reference: unknown;
	currenfy: string;
	// biome-ignore lint/style/useNamingConvention: <api response>
	error_url: string;
	// biome-ignore lint/style/useNamingConvention: <api response>
	last_payment_errror: unknown;
	// biome-ignore lint/style/useNamingConvention: <api response>
	business_name: string;
	// biome-ignore lint/style/useNamingConvention: <api response>
	payment_status: string;
	// biome-ignore lint/style/useNamingConvention: <api response>
	succes_url: string;
	// biome-ignore lint/style/useNamingConvention: <api response>
	wave_launch_url: string;
	// biome-ignore lint/style/useNamingConvention: <api response>
	when_completed: unknown;
	// biome-ignore lint/style/useNamingConvention: <api response>
	when_created: string;
	// biome-ignore lint/style/useNamingConvention: <api response>
	when_expires: string;
}

export interface IwaveStatusResponse {
	status: string;
	code: number;
	message: IwaveStarterRespone;
}

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

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

	// const basictoken = base64.encode("admin:admin");
	try {
		const response = await axiosRequest<IwaveStarterRespone>({
			url: "/transactions/",
			method: "POST",
			headers: {
				// biome-ignore lint/style/useNamingConvention: <explanation>
				Authorization: `Basic ${basictoken}`,
			},
			data: payload,
		});
		log.http("initTransaction |", JSON.stringify(response, null, 2));
		return response;
	} catch (error) {
		log.error("initTransaction |", error);
		throw error;
	}
};

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

	try {
		const response = await axiosRequest<IwaveStarterRespone>({
			url: `/wave-session/${id}/`,
			headers: {
				// biome-ignore lint/style/useNamingConvention: <explanation>
				Authorization: `Basic ${basictoken}`,
			},
		});
		log.http("getTransactionStatus |", JSON.stringify(response, null, 2));

		return response;
	} catch (error) {
		log.error("getTransactionStatus |", error);
		throw error;
	}
};
