<?php
session_start();

echo "<h3>Student ID Debug</h3>";

echo "<p>URL: " . $_SERVER['REQUEST_URI'] . "</p>";
echo "<p>Query String: " . $_SERVER['QUERY_STRING'] . "</p>";

echo "<h4>GET Parameters:</h4>";
echo "<pre>";
print_r($_GET);
echo "</pre>";

$student_id = isset($_GET['id']) ? $_GET['id'] : 'NOT SET';
echo "<p>Raw ID from GET: " . $student_id . "</p>";

$student_id_int = isset($_GET['id']) ? (int)$_GET['id'] : 0;
echo "<p>ID after (int) cast: " . $student_id_int . "</p>";

if ($student_id_int <= 0) {
    echo "<p style='color: red;'>ID is invalid (≤ 0)</p>";
} else {
    echo "<p style='color: green;'>ID is valid (> 0)</p>";
    
    // Check if student exists
    require_once '../config/database.php';
    try {
        $stmt = $pdo->prepare("SELECT id, first_name, last_name, user_id FROM students WHERE id = ?");
        $stmt->execute([$student_id_int]);
        $student = $stmt->fetch();
        
        if ($student) {
            echo "<p style='color: green;'>Student found:</p>";
            echo "<ul>";
            echo "<li>ID: " . $student['id'] . "</li>";
            echo "<li>Name: " . $student['first_name'] . " " . $student['last_name'] . "</li>";
            echo "<li>User ID: " . ($student['user_id'] ? $student['user_id'] : 'NULL') . "</li>";
            echo "</ul>";
        } else {
            echo "<p style='color: red;'>Student not found in database</p>";
        }
    } catch (Exception $e) {
        echo "<p style='color: red;'>Database error: " . $e->getMessage() . "</p>";
    }
}

echo "<h4>Session Info:</h4>";
if (isset($_SESSION['user_id'])) {
    echo "<p>User ID: " . $_SESSION['user_id'] . "</p>";
    echo "<p>User Role: " . $_SESSION['user_role'] . "</p>";
} else {
    echo "<p style='color: red;'>Not logged in</p>";
}
?>