<?php
/**
 * PDO database connection singleton.
 *
 * Provides a lazy-loaded PDO instance configured for the quote wizard
 * database with utf8mb4, exception error mode, and associative fetch.
 */

// Deny direct access
if (basename($_SERVER['SCRIPT_FILENAME'] ?? '') === basename(__FILE__)) {
    http_response_code(403);
    exit('Direct access denied.');
}

require_once __DIR__ . '/config.php';

/**
 * Return a shared PDO connection instance.
 *
 * The connection is created on first call and reused for the lifetime
 * of the request. Uses utf8mb4 charset, ERRMODE_EXCEPTION, and
 * FETCH_ASSOC as the default fetch mode.
 *
 * @return PDO
 * @throws RuntimeException If the connection cannot be established.
 */
function get_db(): PDO
{
    static $pdo = null;

    if ($pdo !== null) {
        return $pdo;
    }

    $dsn = sprintf(
        'mysql:host=%s;dbname=%s;charset=%s',
        DB_HOST,
        DB_NAME,
        DB_CHARSET
    );

    try {
        $pdo = new PDO($dsn, DB_USER, DB_PASS, [
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES   => false,
            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'",
        ]);
    } catch (PDOException $e) {
        app_log('ERROR', 'Database connection failed: ' . $e->getMessage());
        throw new RuntimeException('Database connection failed');
    }

    return $pdo;
}
