<?php
/**
 * Database connection (Core PHP + PDO, no Composer/ORM)
 * Edit the constants below with your cPanel MySQL credentials.
 */

// ---- cPanel MySQL credentials: ai.samirdev.in ----
define('DB_HOST', 'localhost');
define('DB_NAME', 'samirdev_aidatabase');
define('DB_USER', 'samirdev_aiuser');
define('DB_PASS', 'Samir@154198');
define('DB_CHARSET', 'utf8mb4');
// ---------------------------------------------

/**
 * getDbConnection()
 * Returns a shared PDO instance. India timezone is set on every connection
 * so TIMESTAMP/NOW() columns always reflect IST regardless of server default.
 */
function getDbConnection(): PDO
{
    static $pdo = null;

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

    $dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=' . 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,
        ]);
        // Force India Standard Time on this connection (cPanel servers are often UTC)
        $pdo->exec("SET time_zone = '+05:30'");
    } catch (PDOException $e) {
        http_response_code(500);
        header('Content-Type: application/json');
        echo json_encode([
            'success' => false,
            'message' => 'Database connection failed.',
        ]);
        exit;
    }

    return $pdo;
}
