import { Job } from "../core/scheduler";
import { backupsService } from "../modules/backups/backups.service";
import { logger } from "../utils/logger";

export class BackupExecutionJob extends Job {
	async run() {
		logger.debug("Checking for backup schedules to execute...");

		const scheduleIds = await backupsService.getSchedulesToExecute();

		if (scheduleIds.length === 0) {
			logger.debug("No backup schedules to execute");
			return { done: true, timestamp: new Date(), executed: 0 };
		}

		logger.info(`Found ${scheduleIds.length} backup schedule(s) to execute`);

		for (const scheduleId of scheduleIds) {
			backupsService.executeBackup(scheduleId).catch((err) => {
				logger.error(`Error executing backup for schedule ${scheduleId}:`, err);
			});
		}

		return { done: true, timestamp: new Date(), executed: scheduleIds.length };
	}
}
