<?php
// attendance_system/setup.php
echo "<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Attendance System Setup Check</title>
    <link href='https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css' rel='stylesheet'>
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        .container {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            margin-top: 50px;
        }
        .check-list {
            font-family: 'Courier New', monospace;
        }
        .success {
            color: #198754;
        }
        .error {
            color: #dc3545;
        }
        .warning {
            color: #ffc107;
        }
    </style>
</head>
<body>
    <div class='container'>
        <h1 class='text-center mb-4'>🏫 QR Attendance System Setup</h1>
        <div class='alert alert-info'>
            <i class='bi bi-info-circle'></i> Checking system requirements...
        </div>";

echo "<h4 class='mt-4'>📋 System Requirements Check</h4>";
echo "<div class='check-list'>";

// 1. Check PHP version
if (version_compare(PHP_VERSION, '7.4.0') >= 0) {
    echo "<div class='success'>✓ PHP 7.4+ is installed (Current: " . PHP_VERSION . ")</div>";
} else {
    echo "<div class='error'>✗ PHP 7.4+ is required (Current: " . PHP_VERSION . ")</div>";
}

// 2. Check MySQLi extension
if (extension_loaded('mysqli')) {
    echo "<div class='success'>✓ MySQLi extension is enabled</div>";
} else {
    echo "<div class='error'>✗ MySQLi extension is required</div>";
}

// 3. Check session support
if (function_exists('session_start')) {
    echo "<div class='success'>✓ Session support is enabled</div>";
} else {
    echo "<div class='error'>✗ Session support is required</div>";
}

echo "</div>";

echo "<h4 class='mt-4'>🗄️ Database Connection Check</h4>";
echo "<div class='check-list'>";

// Database connection test
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'attendance_system';

$conn = @mysqli_connect($host, $user, $pass);
if ($conn) {
    echo "<div class='success'>✓ Connected to MySQL server</div>";
    
    // Check if database exists
    $db_selected = @mysqli_select_db($conn, $dbname);
    if ($db_selected) {
        echo "<div class='success'>✓ Database '$dbname' exists</div>";
        
        // Check required tables
        $required_tables = [
            'users' => 'Users table',
            'students' => 'Students table',
            'activities' => 'Activities table',
            'attendance' => 'Attendance table',
            'courses' => 'Courses table',
            'departments' => 'Departments table',
            'genders' => 'Genders table',
            'schools' => 'Schools table'
        ];
        
        foreach ($required_tables as $table => $description) {
            $result = @mysqli_query($conn, "SHOW TABLES LIKE '$table'");
            if ($result && mysqli_num_rows($result) > 0) {
                echo "<div class='success'>✓ $description exists</div>";
            } else {
                echo "<div class='error'>✗ $description is missing</div>";
            }
        }
        
        // Check if there are users in the database
        $result = @mysqli_query($conn, "SELECT COUNT(*) as count FROM users");
        if ($result) {
            $row = mysqli_fetch_assoc($result);
            if ($row['count'] > 0) {
                echo "<div class='success'>✓ Users found in database</div>";
                
                // Show sample users
                $result = @mysqli_query($conn, "SELECT username, role, full_name FROM users LIMIT 3");
                if ($result && mysqli_num_rows($result) > 0) {
                    echo "<div class='alert alert-info mt-2'><small>Sample users:</small><br>";
                    while ($user = mysqli_fetch_assoc($result)) {
                        echo "<small>• {$user['username']} ({$user['role']}) - {$user['full_name']}</small><br>";
                    }
                    echo "</div>";
                }
            } else {
                echo "<div class='warning'>⚠ No users found in database</div>";
            }
        }
        
    } else {
        echo "<div class='error'>✗ Database '$dbname' does not exist</div>";
        echo "<div class='alert alert-warning'>";
        echo "Please import the SQL file first:<br>";
        echo "<code>mysql -u root -p attendance_system < attendance_system.sql</code>";
        echo "</div>";
    }
    
    mysqli_close($conn);
} else {
    echo "<div class='error'>✗ Cannot connect to MySQL server</div>";
    echo "<div class='alert alert-danger'>";
    echo "MySQL Error: " . mysqli_connect_error() . "<br>";
    echo "Please check your database credentials in includes/config.php";
    echo "</div>";
}

echo "</div>";

echo "<h4 class='mt-4'>📁 Directory Structure Check</h4>";
echo "<div class='check-list'>";

// Check directory structure
$required_dirs = [
    'includes' => 'Configuration files',
    'assets/css' => 'CSS stylesheets',
    'assets/js' => 'JavaScript files',
    'admin' => 'Admin pages',
    'teacher' => 'Teacher pages',
    'student' => 'Student pages',
    'auth' => 'Authentication pages',
    'api' => 'API endpoints',
    'qr/generated' => 'QR code storage'
];

foreach ($required_dirs as $dir => $description) {
    if (is_dir($dir)) {
        echo "<div class='success'>✓ Directory '$dir' exists</div>";
        
        // Check if directory is writable (for qr/generated)
        if (in_array($dir, ['qr/generated'])) {
            if (is_writable($dir)) {
                echo "<div class='success'>✓ Directory '$dir' is writable</div>";
            } else {
                echo "<div class='warning'>⚠ Directory '$dir' is not writable</div>";
            }
        }
    } else {
        echo "<div class='error'>✗ Directory '$dir' is missing</div>";
    }
}

echo "</div>";

echo "<h4 class='mt-4'>📄 Required Files Check</h4>";
echo "<div class='check-list'>";

// Check required files
$required_files = [
    'includes/config.php' => 'Configuration file',
    'auth/login.php' => 'Login page',
    'admin/dashboard.php' => 'Admin dashboard',
    'admin/attendance.php' => 'QR scanner page',
    'api/scan_qr.php' => 'QR scanning API',
    'index.php' => 'Main entry point'
];

foreach ($required_files as $file => $description) {
    if (file_exists($file)) {
        $size = filesize($file);
        echo "<div class='success'>✓ $description exists (" . round($size/1024, 2) . " KB)</div>";
    } else {
        echo "<div class='error'>✗ $description is missing</div>";
    }
}

echo "</div>";

echo "<h4 class='mt-4'>🚀 Quick Actions</h4>";
echo "<div class='row mt-3'>";
echo "<div class='col-md-4 mb-2'>";
echo "<a href='auth/login.php' class='btn btn-primary w-100'>";
echo "<i class='bi bi-box-arrow-in-right'></i> Go to Login Page";
echo "</a>";
echo "</div>";

echo "<div class='col-md-4 mb-2'>";
echo "<a href='admin/dashboard.php' class='btn btn-success w-100'>";
echo "<i class='bi bi-speedometer2'></i> Admin Dashboard";
echo "</a>";
echo "</div>";

echo "<div class='col-md-4 mb-2'>";
echo "<a href='admin/attendance.php' class='btn btn-info w-100'>";
echo "<i class='bi bi-qr-code-scan'></i> QR Scanner";
echo "</a>";
echo "</div>";
echo "</div>";

echo "<div class='alert alert-success mt-4'>";
echo "<h5><i class='bi bi-check-circle'></i> Setup Instructions:</h5>";
echo "<ol>";
echo "<li>Import the SQL file to your database</li>";
echo "<li>Update database credentials in <code>includes/config.php</code></li>";
echo "<li>Make sure <code>qr/generated/</code> directory is writable</li>";
echo "<li>Access the system via: <code>http://localhost/attendance_system/</code></li>";
echo "<li>Login with default credentials: <strong>johnlloyd12 / password</strong></li>";
echo "</ol>";
echo "</div>";

echo "<div class='alert alert-warning'>";
echo "<h5><i class='bi bi-exclamation-triangle'></i> Security Notes:</h5>";
echo "<ul>";
echo "<li>Change default passwords after first login</li>";
echo "<li>Disable error display in production (set <code>display_errors = Off</code> in php.ini)</li>";
echo "<li>Use HTTPS in production environment</li>";
echo "<li>Regularly backup your database</li>";
echo "</ul>";
echo "</div>";

echo "<div class='text-center mt-4'>";
echo "<small class='text-muted'>QR Attendance System v1.0 | " . date('Y-m-d H:i:s') . "</small>";
echo "</div>";

echo "</div>
<script src='https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js'></script>
</body>
</html>";
?>