<?php
/**
 * EdumacFlo Installer AJAX Dispatcher
 */
header('Content-Type: application/json; charset=utf-8');

require_once __DIR__ . '/Installer.php';

$action = trim($_POST['action'] ?? $_GET['action'] ?? '');

switch ($action) {
    case 'check_requirements':
        $res = Installer::checkRequirements();
        echo json_encode([
            'success'      => true,
            'all_passed'   => $res['all_passed'],
            'requirements' => $res['requirements'],
        ]);
        break;

    case 'test_db':
        $res = Installer::testDatabaseConnection([
            'host'     => $_POST['host'] ?? 'localhost',
            'port'     => (int)($_POST['port'] ?? 3306),
            'username' => $_POST['username'] ?? 'root',
            'password' => $_POST['password'] ?? '',
            'dbname'   => $_POST['dbname'] ?? '',
        ]);
        echo json_encode($res);
        break;

    case 'install':
        $res = Installer::executeInstallation($_POST);
        echo json_encode($res);
        break;

    default:
        echo json_encode([
            'success' => false,
            'message' => 'Invalid action requested.',
        ]);
        break;
}
