<?php
// Database Configuration
// Compatible with PHP 8.2 and works for both Localhost and Live Server

$isLocalhost = in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1']) || (isset($_SERVER['HTTP_HOST']) && str_contains($_SERVER['HTTP_HOST'], 'localhost'));

if ($isLocalhost) {
    // Localhost Settings (Standard XAMPP/MAMP Defaults)
    $host = '127.0.0.1';
    $db   = 'ibvte';
    $user = 'root'; 
    $pass = '';
} else {
    // Live Server Settings
    $host = 'localhost';
    $db   = 'flightbo_ibvte';
    $user = 'flightbo_ibvte';
    $pass = 'flightbo_ibvte2';
}

$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];

try {
     $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     // In production, you might want to hide this and log it instead
     if ($isLocalhost) {
         die("Database Connection Error: " . $e->getMessage());
     } else {
         die("<div style='padding:20px; font-family:sans-serif; text-align:center;'>
              <h3 style='color:red;'>Database Connection Failed!</h3>
              <p>cPanel is rejecting your connection. Ensure that the user <b>flightbo_ibvte</b> is added to the database <b>flightbo_ibvte</b> with ALL PRIVILEGES.</p>
              <p><b>Exact cPanel Error:</b> " . htmlspecialchars($e->getMessage()) . "</p>
              </div>");
     }
}
?>
