<?php
// Enable error reporting to see what's causing the 500 error
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h2>Debugging leave_applications.php</h2>";

// Check if database connection works
echo "<h3>1. Testing database connection...</h3>";
require_once 'config/database.php';

if (isset($pdo)) {
    echo "<p style='color: green;'>✓ Database connection successful</p>";
} else {
    echo "<p style='color: red;'>✗ Database connection failed</p>";
    exit;
}

// Check if teacher_leaves table exists
echo "<h3>2. Checking if teacher_leaves table exists...</h3>";
try {
    $stmt = $pdo->query("SHOW TABLES LIKE 'teacher_leaves'");
    if ($stmt->rowCount() > 0) {
        echo "<p style='color: green;'>✓ teacher_leaves table exists</p>";
        
        // Check table structure
        echo "<h3>3. Table structure:</h3>";
        $stmt = $pdo->query("DESCRIBE teacher_leaves");
        $columns = $stmt->fetchAll();
        echo "<ul>";
        foreach ($columns as $col) {
            echo "<li>{$col['Field']} - {$col['Type']} - {$col['Null']} - {$col['Key']}</li>";
        }
        echo "</ul>";
    } else {
        echo "<p style='color: red;'>✗ teacher_leaves table does not exist</p>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>Error checking table: " . $e->getMessage() . "</p>";
}

// Check if teachers table exists
echo "<h3>4. Checking if teachers table exists...</h3>";
try {
    $stmt = $pdo->query("SHOW TABLES LIKE 'teachers'");
    if ($stmt->rowCount() > 0) {
        echo "<p style='color: green;'>✓ teachers table exists</p>";
        
        // Check teacher_leaves records
        echo "<h3>5. Checking teacher_leaves records...</h3>";
        try {
            $stmt = $pdo->query("SELECT COUNT(*) as count FROM teacher_leaves");
            $count = $stmt->fetch();
            echo "<p>Total records in teacher_leaves: {$count['count']}</p>";
            
            if ($count['count'] > 0) {
                $stmt = $pdo->query("
                    SELECT tl.*, t.first_name, t.last_name, t.employee_id 
                    FROM teacher_leaves tl 
                    JOIN teachers t ON tl.teacher_id = t.id 
                    LIMIT 5
                ");
                $records = $stmt->fetchAll();
                echo "<h4>Sample records:</h4>";
                foreach ($records as $record) {
                    echo "<p>ID: {$record['id']}, Teacher: {$record['first_name']} {$record['last_name']}, Status: {$record['status']}</p>";
                }
            }
        } catch (Exception $e) {
            echo "<p style='color: red;'>Error querying teacher_leaves: " . $e->getMessage() . "</p>";
        }
    } else {
        echo "<p style='color: red;'>✗ teachers table does not exist</p>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>Error checking teachers table: " . $e->getMessage() . "</p>";
}

// Test the query that's used in leave_applications.php
echo "<h3>6. Testing the main query from leave_applications.php...</h3>";
try {
    $stmt = $pdo->prepare("
        SELECT tl.*, t.first_name, t.last_name, t.employee_id 
        FROM teacher_leaves tl 
        JOIN teachers t ON tl.teacher_id = t.id 
        ORDER BY tl.applied_date DESC
    ");
    $stmt->execute();
    $leave_applications = $stmt->fetchAll();
    echo "<p style='color: green;'>✓ Query executed successfully, found " . count($leave_applications) . " records</p>";
} catch (Exception $e) {
    echo "<p style='color: red;'>Error with main query: " . $e->getMessage() . "</p>";
}

echo "<h3>7. Testing authentication...</h3>";
session_start();
if (isset($_SESSION['user_id'])) {
    echo "<p>Session user_id: " . $_SESSION['user_id'] . "</p>";
    echo "<p>Session user_role: " . ($_SESSION['user_role'] ?? 'not set') . "</p>";
    
    if ($_SESSION['user_role'] === 'admin') {
        echo "<p style='color: green;'>✓ User has admin role</p>";
    } else {
        echo "<p style='color: orange;'>User role is not admin (required for this page)</p>";
    }
} else {
    echo "<p style='color: red;'>✗ No user is logged in</p>";
}
?>