<?php
/**
 * AJAX Backup Now Handler
 * Handles manual backup execution with JSON response
 */

// Set longer execution time for backup process
set_time_limit(300); // 5 minutes

// Set JSON content type
header('Content-Type: application/json');

try {
    // Change working directory to parent to ensure relative paths work
    chdir(dirname(__DIR__));
    
    // Include backup engine (which includes wamp_detector)
    include 'backup_engine.php';
    
    // Create backup engine instance
    $engine = new DatabaseBackupEngine();
    
    // Run manual backup
    $result = $engine->createBackup('manual');
    
    // Return JSON response
    if ($result['success']) {
        echo json_encode([
            'success' => true,
            'title' => 'Backup Completed Successfully!',
            'message' => "📁 File: {$result['filename']}\n📊 Size: {$result['size']}\n⏱️ Duration: {$result['duration']}s" . 
                        ($result['compression_ratio'] > 0 ? "\n🗜️ Compression: {$result['compression_ratio']}%" : ""),
            'filename' => $result['filename'],
            'size' => $result['size'],
            'duration' => $result['duration'],
            'compression_ratio' => $result['compression_ratio']
        ]);
    } else {
        echo json_encode([
            'success' => false,
            'title' => 'Backup Failed',
            'message' => $result['message'],
            'error' => $result['error'] ?? 'Unknown error'
        ]);
    }
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'title' => 'Fatal Error',
        'message' => $e->getMessage(),
        'error' => 'Exception: ' . $e->getMessage()
    ]);
}
?>
