<?php
/**
 * AJAX Handler for Windows Task Scheduler Management
 */

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

try {
    // Change working directory to parent
    chdir(dirname(__DIR__));
    
    // Include the Windows Task Manager
    require_once 'handlers/windows_task_manager.php';
    
    $action = $_POST['action'] ?? $_GET['action'] ?? '';
    $taskManager = new WindowsTaskManager();
    
    switch ($action) {
        case 'create':
            $result = $taskManager->createBackupTask();
            break;
            
        case 'remove':
            $result = $taskManager->removeBackupTask();
            break;
            
        case 'status':
            $isRunning = $taskManager->isTaskRunning();
            $taskStatus = $taskManager->getTaskStatus();
            $systemInfo = $taskManager->getSystemInfo();
            
            $result = [
                'success' => true,
                'task_running' => $isRunning,
                'task_status' => $taskStatus,
                'system_info' => $systemInfo
            ];
            break;
            
        case 'test':
            $systemInfo = $taskManager->getSystemInfo();
            $result = [
                'success' => true,
                'system_info' => $systemInfo,
                'schtasks_available' => $taskManager->isSchtasksAvailable()
            ];
            break;
            
        default:
            $result = [
                'success' => false,
                'message' => 'Invalid action specified'
            ];
    }
    
    echo json_encode($result);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => 'Error: ' . $e->getMessage()
    ]);
}
?>
