<?php
// Database configuration for UK Data Services
// Use these settings for MySQL connection

// Docker container database settings
define('DB_HOST', 'ukdataservices-db'); // or 'localhost' if connecting from outside Docker
define('DB_USER', 'webuser');           // Use webuser, not root
define('DB_PASSWORD', 'webpassword');   // Password from docker-compose.yml
define('DB_NAME', 'ukdataservices');    // Database name
define('DB_PORT', 3306);

// Alternative root credentials (if needed)
define('DB_ROOT_USER', 'root');
define('DB_ROOT_PASSWORD', 'rootpassword');

// Connection function
function getDBConnection($useRoot = false) {
    try {
        if ($useRoot) {
            $pdo = new PDO(
                "mysql:host=" . DB_HOST . ";port=" . DB_PORT . ";dbname=" . DB_NAME,
                DB_ROOT_USER,
                DB_ROOT_PASSWORD,
                [
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"
                ]
            );
        } else {
            $pdo = new PDO(
                "mysql:host=" . DB_HOST . ";port=" . DB_PORT . ";dbname=" . DB_NAME,
                DB_USER,
                DB_PASSWORD,
                [
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"
                ]
            );
        }
        return $pdo;
    } catch (PDOException $e) {
        error_log("Database connection failed: " . $e->getMessage());
        return false;
    }
}

// Test connection function
function testConnection() {
    echo "<h2>Testing Database Connections</h2>";
    
    // Test webuser connection
    echo "<h3>Testing webuser connection:</h3>";
    $pdo = getDBConnection(false);
    if ($pdo) {
        echo "<span style='color: green;'>✓ webuser connection successful</span><br>";
        try {
            $stmt = $pdo->query("SELECT DATABASE() as current_db, USER() as current_user");
            $result = $stmt->fetch();
            echo "Current database: " . $result['current_db'] . "<br>";
            echo "Current user: " . $result['current_user'] . "<br>";
        } catch (Exception $e) {
            echo "<span style='color: red;'>Error querying database: " . $e->getMessage() . "</span><br>";
        }
    } else {
        echo "<span style='color: red;'>✗ webuser connection failed</span><br>";
    }
    
    echo "<br>";
    
    // Test root connection
    echo "<h3>Testing root connection:</h3>";
    $pdo_root = getDBConnection(true);
    if ($pdo_root) {
        echo "<span style='color: green;'>✓ root connection successful</span><br>";
        try {
            $stmt = $pdo_root->query("SHOW DATABASES");
            $databases = $stmt->fetchAll();
            echo "Available databases: ";
            foreach ($databases as $db) {
                echo $db['Database'] . " ";
            }
            echo "<br>";
        } catch (Exception $e) {
            echo "<span style='color: red;'>Error querying databases: " . $e->getMessage() . "</span><br>";
        }
    } else {
        echo "<span style='color: red;'>✗ root connection failed</span><br>";
    }
}

// Uncomment the line below to test connections when accessing this file directly
// if (basename(__FILE__) == basename($_SERVER['PHP_SELF'])) testConnection();
?>
