<?php
// cron/backup.php — Toutes les 6h
// Backup DB mykollufab — rétention 3 niveaux — upload Dropbox hebdo lundi

require_once __DIR__ . '/_bootstrap.php';

use App\Services\BackupService;
use App\Services\MailService;

$backup = new BackupService();
$mail   = new MailService();

try {
    $filepath = $backup->creerBackup();
    error_log('[CRON backup] ' . date('c') . ' — Backup créé : ' . basename($filepath));

    // Upload Dropbox uniquement le lundi (hebdomadaire)
    if (date('N') === '1') {
        try {
            $backup->uploadDropbox($filepath);
            error_log('[CRON backup] Upload Dropbox OK : ' . basename($filepath));
        } catch (\Exception $e) {
            error_log('[CRON backup] Upload Dropbox ÉCHEC : ' . $e->getMessage());
            $mail->backupDropboxErreur($e->getMessage());
        }
    }
} catch (\Exception $e) {
    error_log('[CRON backup] ERREUR : ' . $e->getMessage());
    $mail->backupDropboxErreur($e->getMessage());
}
