<?php
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h2>System Setup Check</h2>";

require_once 'config/database.php';

echo "<h3>1. Checking Database Tables</h3>";

// Check if required tables exist
$tables = ['users', 'teachers', 'teacher_leaves'];
foreach ($tables as $table) {
    try {
        $stmt = $pdo->query("SHOW TABLES LIKE '$table'");
        if ($stmt->rowCount() > 0) {
            echo "<p style='color: green;'>✓ $table table exists</p>";
            
            // Count records in each table
            $countStmt = $pdo->query("SELECT COUNT(*) as count FROM $table");
            $count = $countStmt->fetch();
            echo "<p>&nbsp;&nbsp;&nbsp; Records in $table: {$count['count']}</p>";
        } else {
            echo "<p style='color: red;'>✗ $table table does not exist</p>";
        }
    } catch (Exception $e) {
        echo "<p style='color: red;'>Error checking $table: " . $e->getMessage() . "</p>";
    }
}

echo "<h3>2. Checking Admin User</h3>";
try {
    $stmt = $pdo->query("SELECT * FROM users WHERE role = 'admin' LIMIT 1");
    $admin = $stmt->fetch();
    if ($admin) {
        echo "<p style='color: green;'>✓ Admin user exists: " . $admin['username'] . "</p>";
    } else {
        echo "<p style='color: orange;'>No admin user found. You may need to create one.</p>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>Error checking admin user: " . $e->getMessage() . "</p>";
}

echo "<h3>3. Checking Teacher Users</h3>";
try {
    $stmt = $pdo->query("SELECT u.username, u.role, t.first_name, t.last_name, t.employee_id FROM users u LEFT JOIN teachers t ON u.id = t.user_id WHERE u.role = 'teacher' LIMIT 5");
    $teachers = $stmt->fetchAll();
    if (count($teachers) > 0) {
        echo "<p style='color: green;'>✓ Found " . count($teachers) . " teacher users</p>";
        foreach ($teachers as $teacher) {
            if ($teacher['first_name']) {
                echo "<p>&nbsp;&nbsp;&nbsp; ✓ " . $teacher['username'] . " (" . $teacher['first_name'] . " " . $teacher['last_name'] . ", ID: " . $teacher['employee_id'] . ")</p>";
            } else {
                echo "<p>&nbsp;&nbsp;&nbsp; ✗ " . $teacher['username'] . " (No matching record in teachers table)</p>";
            }
        }
    } else {
        echo "<p style='color: orange;'>No teacher users found. You may need to create some.</p>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>Error checking teacher users: " . $e->getMessage() . "</p>";
}

echo "<h3>4. Login Instructions</h3>";
echo "<p>If pages are showing 500 errors, it's likely due to one of these reasons:</p>";
echo "<ul>";
echo "<li>You're not logged in - visit <a href='login.php'>login.php</a> first</li>";
echo "<li>You're logged in with the wrong role - admin pages need admin login, teacher pages need teacher login</li>";
echo "<li>Your user account doesn't have a corresponding record in the teachers table (for teacher pages)</li>";
echo "</ul>";

echo "<h3>5. Default Login Credentials</h3>";
echo "<p>Default admin login: admin / admin123</p>";
echo "<p>To create a teacher user, you would typically need to:</p>";
echo "<ol>";
echo "<li>Login as admin</li>";
echo "<li>Go to teacher management section</li>";
echo "<li>Create a new teacher user</li>";
echo "<li>That user can then log in and access teacher pages</li>";
echo "</ol>";

echo "<p><a href='login.php' style='color: blue; text-decoration: underline;'>Go to Login Page</a></p>";
?>