import { isUmamiEnabled, umamiConfig } from "../configs/umami.config.js";

type TrackLstEventInput = {
	eventName: string;
	eventData?: Record<string, unknown>;
	url?: string;
	hostname?: string;
};

export async function trackLstEvent({
	eventName,
	eventData,
	url = "/backend",
	hostname = umamiConfig.server,
}: TrackLstEventInput): Promise<void> {
	if (!isUmamiEnabled()) return;

	try {
		await fetch(`${umamiConfig.umamiHost}/api/send`, {
			method: "POST",
			headers: {
				"Content-Type": "application/json",
				"User-Agent": "LST-Backend",
			},
			body: JSON.stringify({
				type: "event",
				payload: {
					website: umamiConfig.umamiWebsiteId,
					name: eventName,
					url,
					hostname,
					language: "en-US",
					screen: "backend",
					data: {
						app: umamiConfig.appName,
						site: umamiConfig.site,
						server: umamiConfig.server,
						appVersion: umamiConfig.appVersion,
						source: "backend",
						...eventData,
					},
				},
			}),
		});
	} catch (err) {
		console.error("Failed to send Umami backend event", err);
	}
}

/*
await trackLstEvent({
	eventName: "label_print_completed",
	url: "/backend/printers",
	eventData: {
		module: "printers",
		printerName,
		labelType,
	},
});

*/
