<?php
// ── PDO Database Connection ──────────────────────────────────
function getDBConnection(): PDO {
    static $pdo = null;
    if ($pdo !== null) return $pdo;

    // ── Update these with your cPanel / hosting credentials ──
    $host = 'localhost';          // usually 'localhost' on shared hosting
    $db   = 'virenent_upclb';    // your cPanel database name
    $user = 'virenent_upclb';    // your cPanel database username
    $pass = 'Deepak9955@';                   // your database password (set in cPanel)

    try {
        $pdo = new PDO(
            "mysql:host={$host};dbname={$db};charset=utf8mb4",
            $user, $pass,
            [
                PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                PDO::ATTR_EMULATE_PREPARES   => false,
            ]
        );
    } catch (PDOException $e) {
        http_response_code(500);
        die('<div style="font-family:sans-serif;padding:30px;color:#c62828;background:#ffebee;border-left:4px solid #c62828;margin:20px;">
            <strong>Database Connection Failed</strong><br>
            Please check <code>config/db.php</code> settings.<br>
            <small>' . htmlspecialchars($e->getMessage()) . '</small>
        </div>');
    }
    return $pdo;
}
