import { createLogger } from "../logger/logger.controller.js";

const minTime = 5;
export const minutesToCron = (minutes: number): string => {
	const log = createLogger({ module: "system", subModule: "croner" });
	if (minutes < minTime) {
		log.error(
			{},
			`Conversion of less then ${minTime} min and is not allowed on this server`,
		);
	}

	// Every X minutes (under 60)
	if (minutes < 60) {
		return `*/${minutes} * * * *`;
	}

	// Every X hours (clean division)
	if (minutes % 60 === 0 && minutes < 1440) {
		const hours = minutes / 60;
		return `0 0 */${hours} * * *`;
	}

	// Every day
	if (minutes === 1440) {
		return `0 0 8 * * *`; // 8am
	}

	// Every X days (clean division)
	if (minutes % 1440 === 0) {
		const days = minutes / 1440;
		return `0 0 0 */${days} * *`;
	}

	// Fallback (not cleanly divisible)
	// Cron can't represent this perfectly → run every X minutes as approximation
	return `0 */${minutes} * * * *`;
};
