<?php
/**
 * Debug script for buses.php page
 */

// Start output buffering to capture any errors
ob_start();

// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/config/database.php';

echo "<h2>Buses Page Debug Report</h2>";

try {
    // Check if buses table exists
    $result = $pdo->query("SHOW TABLES LIKE 'buses'");
    $buses_table_exists = $result->rowCount() > 0;

    if ($buses_table_exists) {
        echo "<p style='color: green;'>✅ buses table exists</p>";
        
        // Count records in buses table
        $count = $pdo->query("SELECT COUNT(*) as count FROM buses")->fetch();
        echo "<p>Records in buses table: {$count['count']}</p>";
    } else {
        echo "<p style='color: red;'>❌ buses table does not exist</p>";
        
        // Let's try to create it again
        echo "<h3>Trying to create buses table...</h3>";
        try {
            $pdo->exec("
                CREATE TABLE IF NOT EXISTS buses (
                    id INT AUTO_INCREMENT PRIMARY KEY,
                    bus_number VARCHAR(20) UNIQUE NOT NULL,
                    bus_name VARCHAR(100),
                    model VARCHAR(50),
                    capacity INT DEFAULT 50,
                    driver_name VARCHAR(100),
                    driver_phone VARCHAR(20),
                    conductor_name VARCHAR(100),
                    conductor_phone VARCHAR(20),
                    registration_number VARCHAR(50),
                    insurance_expiry DATE,
                    fitness_certificate_expiry DATE,
                    status ENUM('active', 'maintenance', 'retired') DEFAULT 'active',
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
                )
            ");
            echo "<p style='color: green;'>✅ buses table created successfully!</p>";
        } catch (Exception $e) {
            echo "<p style='color: red;'>❌ Error creating buses table: " . $e->getMessage() . "</p>";
        }
    }

    // Check if user is logged in by checking session
    session_start();
    if (isset($_SESSION['user_id'])) {
        echo "<p style='color: green;'>✅ User is logged in (user_id: {$_SESSION['user_id']})</p>";
        echo "<p>User role: {$_SESSION['user_role']}</p>";
    } else {
        echo "<p style='color: orange;'>⚠️ User is not logged in - you need to log in first</p>";
        echo "<p>Please <a href='login.php'>click here to log in</a> with admin credentials (default: admin/admin123)</p>";
    }

    // Check if the buses.php file exists
    $buses_file = __DIR__ . '/admin/transport/buses.php';
    if (file_exists($buses_file)) {
        echo "<p style='color: green;'>✅ buses.php file exists</p>";
        $file_size = filesize($buses_file);
        echo "<p>File size: {$file_size} bytes</p>";
    } else {
        echo "<p style='color: red;'>❌ buses.php file does not exist at expected location</p>";
    }

    // Check if all required includes exist
    $required_files = [
        __DIR__ . '/config/database.php',
        __DIR__ . '/includes/SecurityHelper.php'
    ];
    
    foreach ($required_files as $file) {
        if (file_exists($file)) {
            echo "<p style='color: green;'>✅ Required file exists: " . basename($file) . "</p>";
        } else {
            echo "<p style='color: red;'>❌ Required file missing: " . basename($file) . "</p>";
        }
    }

    // Show any captured output
    $buffered_content = ob_get_contents();
    if (!empty($buffered_content)) {
        echo "<h3>Buffered Output:</h3><pre>$buffered_content</pre>";
    }
    ob_end_clean();

} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Error during debug: " . $e->getMessage() . "</p>";
    echo "<p>Trace: <pre>" . $e->getTraceAsString() . "</pre></p>";
}

echo "<h3>Navigation Options:</h3>";
echo "<ul>";
echo "<li><a href='index.php'>🏠 Go to Home Page</a></li>";
echo "<li><a href='login.php'>🔐 Login Page</a> (if not logged in, use default: admin/admin123)</li>";
echo "<li><a href='admin/transport/buses.php'>🚌 Buses Management Page</a> (after logging in)</li>";
echo "</ul>";

echo "<h3>Next Steps:</h3>";
echo "<ol>";
echo "<li>If you're not logged in, please <a href='login.php'>log in first</a> with admin credentials (default: admin/admin123)</li>";
echo "<li>After logging in, try accessing <a href='admin/transport/buses.php'>the buses page again</a></li>";
echo "<li>If tables were missing, they should now be created - refresh the buses page</li>";
echo "</ol>";
?>