<?php
/**
 * config/database.php
 *
 * Database configuration and PDO instance.
 */

// Database configuration
define('DB_HOST', 'localhost');
define('DB_NAME', 'devcrop');
define('DB_USER', 'devcrop');
define('DB_PASS', 'PGrspJH7NBbNiJYMbEYRHIJHw');
define('DB_CHARSET', 'utf8');

// Create PDO instance
try {
    $pdo = new PDO(
        "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=" . DB_CHARSET,
        DB_USER,
        DB_PASS,
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES => false,
        ]
    );
} catch (PDOException $e) {
    error_log("Database connection failed: " . $e->getMessage());
    die("Database connection failed. Please try again later.");
}

/**
 * Return a shared PDO connection instance.
 *
 * @return PDO
 */
function getDBConnection(): PDO
{
    global $pdo;
    if (!isset($pdo)) {
        throw new RuntimeException('Database connection not initialized');
    }
    return $pdo;
}

?>