import type { InferResultType } from "@/utils/drizzle";

export function calculatePrinterUtilization(jobs: InferResultType<"printJobs", { printer: true }>[]) {
	const printers = jobs.reduce<Record<string, string>>((acc, job) => {
		acc[job.printerId] = job.printer.name;
		return acc;
	}, {});

	const usedTimePerPrinter: Record<string, number> = jobs.reduce(
		(acc, job) => {
			acc[job.printer.id] = (acc[job.printer.id] || 0) + job.durationInMinutes;

			return acc;
		},
		{} as Record<string, number>,
	);

	const totalTimeInMinutes = 60 * 35 * 3; // 60 Minutes * 35h * 3 Weeks
	// 35h Woche, 3 mal in der Woche in TBA

	const printerUtilizationPercentage = Object.keys(usedTimePerPrinter).map((printerId) => {
		const usedTime = usedTimePerPrinter[printerId];

		return {
			printerId,
			name: printers[printerId],
			utilizationPercentage: usedTime / totalTimeInMinutes,
		};
	});

	return printerUtilizationPercentage;
}
