<?php
session_start();
$basePath = dirname(__DIR__);
if (file_exists($basePath . '/config.php') || file_exists($basePath . '/install/install.lock')) { die('Installer is locked. To reinstall safely, back up the database, remove config.php and install/install.lock manually, then reload this page.'); }
$step = 2;
$stepNames = ['Welcome', 'Requirements', 'Permissions', 'Database', 'Import', 'Academy', 'Banking', 'Admin'];

$checks = [
    ['PHP Version >= 8.1', version_compare(PHP_VERSION, '8.1.0', '>='), PHP_VERSION],
    ['PDO Extension', extension_loaded('pdo'), ''],
    ['PDO MySQL Driver', extension_loaded('pdo_mysql'), ''],
    ['MBString Extension', extension_loaded('mbstring'), ''],
    ['JSON Extension', extension_loaded('json'), ''],
    ['GD Extension', extension_loaded('gd'), ''],
    ['ZIP Extension', extension_loaded('zip'), ''],
    ['cURL Extension', extension_loaded('curl'), ''],
    ['OpenSSL Extension', extension_loaded('openssl'), ''],
    ['Fileinfo Extension', extension_loaded('fileinfo'), ''],
    ['Session Support', function_exists('session_start'), ''],
    ['Bundled Runtime Libraries', file_exists($basePath . '/app/bootstrap.php') && file_exists($basePath . '/app/Libraries/autoload.php'), 'Shared-hosting mode'],
];
$allPassed = true;
foreach ($checks as $c) { if (!$c[1]) $allPassed = false; }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CIX Orbit Academy - System Requirements</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <link rel="stylesheet" href="assets/css/install.css">
</head>
<body>
<div class="installer-wrapper">
    <div class="installer-card">
        <div class="installer-header">
            <h1><i class="fas fa-orbit"></i> CIX <span>Orbit</span> Academy</h1>
            <p>Complete LMS &amp; Student Management System</p>
        </div>
        <div class="step-indicator">
            <?php for ($i = 1; $i <= 8; $i++): ?>
            <div class="step-dot <?= $i === $step ? 'active' : ($i < $step ? 'completed' : '') ?>">
                <?= $i < $step ? '<i class="fas fa-check"></i>' : $i ?>
                <span class="step-label"><?= $stepNames[$i-1] ?></span>
            </div>
            <?php endfor; ?>
        </div>
        <div class="installer-content">
            <h2><i class="fas fa-cogs" style="color:var(--cix-orange)"></i> System Requirements</h2>
            <p class="subtitle">Checking your server environment...</p>
            <?php foreach ($checks as $i => $c): ?>
            <div class="check-item">
                <div>
                    <div class="check-name"><?= $c[0] ?></div>
                    <?php if ($c[2]): ?><div class="check-detail">Current: <?= $c[2] ?></div><?php endif; ?>
                </div>
                <span class="check-status <?= $c[1] ? 'pass' : 'fail' ?>"><?= $c[1] ? 'PASSED' : 'FAILED' ?></span>
            </div>
            <?php endforeach; ?>
            <?php if ($allPassed): ?>
            <div class="alert alert-success"><i class="fas fa-check-circle"></i> All requirements met! Your server can run CIX Orbit Academy.</div>
            <div class="btn-group">
                <a href="index.php" class="btn btn-secondary"><i class="fas fa-arrow-left"></i> Back</a>
                <a href="step3.php" class="btn btn-primary">Continue <i class="fas fa-arrow-right"></i></a>
            </div>
            <?php else: ?>
            <div class="alert alert-error"><i class="fas fa-exclamation-circle"></i> Some requirements are not met. Please fix the failed items and try again.</div>
            <div class="btn-group">
                <a href="index.php" class="btn btn-secondary"><i class="fas fa-arrow-left"></i> Back</a>
                <a href="#" class="btn btn-disabled"><i class="fas fa-lock"></i> Fix Issues to Continue</a>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>
<script src="assets/js/install.js"></script>
</body>
</html>
