<?php
require_once __DIR__ . '/../../app/includes/auth.php';
require_auth();
require_admin();
require_once __DIR__ . '/../../app/services/DatabaseBackupService.php';
try {
    $filename = (string)($_GET['file'] ?? '');
    $path = DatabaseBackupService::resolve($filename);
    log_activity('database_backup_downloaded', 'Downloaded database backup ' . basename($path));
    header('Content-Type: application/sql');
    header('Content-Disposition: attachment; filename="' . basename($path) . '"');
    header('Content-Length: ' . filesize($path));
    readfile($path);
    exit;
} catch (Throwable $e) {
    flash('error', $e->getMessage());
    redirect('/backups/index.php');
}
