<?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 = 3;
$stepNames = ['Welcome', 'Requirements', 'Permissions', 'Database', 'Import', 'Academy', 'Banking', 'Admin'];

$folders = ['config', 'storage', 'storage/logs', 'storage/cache', 'storage/certificates', 'storage/reports', 'storage/proofs', 'storage/lesson-files', 'storage/backups', 'public/uploads', 'public/assets/images'];

$folderResults = [];
foreach ($folders as $folder) {
    $path = $basePath . '/' . $folder;
    $exists = is_dir($path);
    $writable = false;
    if ($exists) {
        $writable = is_writable($path);
        if (!$writable) {
            @chmod($path, 0755);
            $writable = is_writable($path);
        }
    } else {
        @mkdir($path, 0755, true);
        $exists = is_dir($path);
        $writable = $exists && is_writable($path);
    }
    $folderResults[] = ['folder' => $folder, 'exists' => $exists, 'writable' => $writable];
}
?>
<!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 - Folder Permissions</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-folder-open" style="color:var(--cix-orange)"></i> Folder Permissions</h2>
            <p class="subtitle">Checking directory write permissions...</p>
            <?php foreach ($folderResults as $fr): ?>
            <div class="check-item">
                <div>
                    <div class="check-name"><code>/<?= $fr['folder'] ?></code></div>
                    <div class="check-detail"><?= $fr['exists'] ? 'Exists' : 'Created' ?></div>
                </div>
                <span class="check-status <?= $fr['writable'] ? 'pass' : 'fail' ?>"><?= $fr['writable'] ? 'WRITABLE' : 'NOT WRITABLE' ?></span>
            </div>
            <?php endforeach; ?>
            <div class="alert alert-info"><i class="fas fa-info-circle"></i> Folders will be created automatically if they don't exist. Ensure your hosting allows write access.</div>
            <div class="btn-group">
                <a href="step2.php" class="btn btn-secondary"><i class="fas fa-arrow-left"></i> Back</a>
                <a href="step4.php" class="btn btn-primary">Continue <i class="fas fa-arrow-right"></i></a>
            </div>
        </div>
    </div>
</div>
<script src="assets/js/install.js"></script>
</body>
</html>
