<?php

declare(strict_types=1);

/**
 * Database Configuration and Connection
 * 
 * Provides a PDO singleton connection for the Gmind Institute application.
 * Uses utf8mb4 charset for full Unicode support including emojis.
 */

// Database credentials
const DB_HOST    = 'localhost';
const DB_NAME    = 'advaysin_gmind';
const DB_USER    = 'advaysin_gmind';
const DB_PASS    = 'Kk@9305640290';
const DB_CHARSET = 'utf8mb4';

/**
 * Get a PDO database connection (singleton pattern)
 *
 * Returns the same PDO instance on subsequent calls to avoid
 * creating multiple connections to the database.
 *
 * @return PDO
 * @throws PDOException If the connection fails
 */
function getDb(): PDO
{
    static $pdo = null;

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

        $options = [
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES   => false,
            PDO::ATTR_PERSISTENT         => false,
        ];

        $pdo = new PDO($dsn, DB_USER, DB_PASS, $options);
    }

    return $pdo;
}
