<?php
/**
 * Database Connection Test Script
 * Run this file directly to test database connection
 */

// Display errors for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h1>Ruhondo Beach Resort HMS - Database Connection Test</h1>";

// Define BASE_PATH for the database file
define('BASE_PATH', __DIR__);

try {
    // Load configuration first
    echo "<p>Loading configuration...</p>";
    require_once  '/../config/config.php';
    echo "<p style='color: green;'>✓ Configuration loaded successfully</p>";
    
    // Now load database functions
    echo "<p>Loading database functions...</p>";
    require_once  '/config/database.php';
    echo "<p style='color: green;'>✓ Database functions loaded successfully</p>";
    
    // Test database connection
    echo "<p>Testing database connection...</p>";
    
    if (function_exists('test_db_connection')) {
        if (test_db_connection()) {
            echo "<p style='color: green; font-weight: bold;'>✓ Database connection successful!</p>";
        } else {
            echo "<p style='color: red; font-weight: bold;'>✗ Database connection failed!</p>";
        }
    } else {
        // Manual test if function doesn't exist
        try {
            $pdo = get_db_connection();
            $result = $pdo->query("SELECT 1")->fetch();
            echo "<p style='color: green; font-weight: bold;'>✓ Database connection successful! (Manual test)</p>";
        } catch (Exception $e) {
            echo "<p style='color: red; font-weight: bold;'>✗ Database connection failed: " . $e->getMessage() . "</p>";
        }
    }
    
    // Get database info
    echo "<h2>Database Information</h2>";
    echo "<ul>";
    echo "<li><strong>Host:</strong> " . DB_HOST . "</li>";
    echo "<li><strong>Database:</strong> " . DB_NAME . "</li>";
    echo "<li><strong>User:</strong> " . DB_USER . "</li>";
    echo "<li><strong>Charset:</strong> " . DB_CHARSET . "</li>";
    echo "</ul>";
    
    // Test a simple query
    echo "<p>Testing simple query...</p>";
    $tables = db_fetch_all("SHOW TABLES");
    echo "<p style='color: green;'>✓ Query executed successfully</p>";
    echo "<p>Found " . count($tables) . " tables in database:</p>";
    echo "<ul>";
    foreach ($tables as $table) {
        $table_name = current($table);
        echo "<li>" . $table_name . "</li>";
    }
    echo "</ul>";
    
} catch (Exception $e) {
    echo "<p style='color: red; font-weight: bold;'>Error: " . $e->getMessage() . "</p>";
    echo "<pre>" . $e->getTraceAsString() . "</pre>";
}