<?php

define('DB_HOST',     'localhost');
define('DB_USER',     'root');
define('DB_PASSWORD', '');
define('DB_NAME',     'shopnest_db');
define('DB_CHARSET',  'utf8mb4');

/**
 * Returns a singleton MySQLi connection.
 */
function getDb(): mysqli {
    static $conn = null;

    if ($conn === null) {
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        try {
            $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
            $conn->set_charset(DB_CHARSET);
            $conn->query("SET time_zone = '+05:30'");
        } catch (mysqli_sql_exception $e) {
            error_log('DB Connection failed: ' . $e->getMessage());
            sendError('Database connection failed', 500);
            exit();
        }
    }

    return $conn;
}
