<?php
// ── CY Consultancy Centre — Frontend Config ──
ob_start();
if (session_status() === PHP_SESSION_NONE) session_start();

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') ? 'https' : 'http';
$host     = $_SERVER['HTTP_HOST'];
$script   = $_SERVER['SCRIPT_NAME'];
$dir      = dirname($script);

define('BASE_URL',  $protocol.'://'.$host.rtrim($dir,'/'));
define('BASE_PATH', rtrim($dir,'/')  . '/');
define('ROOT_DIR',  str_replace('\\','/',dirname(__DIR__)));

// ── Database (same as admin) ──
define('DB_HOST', 'localhost');
define('DB_USER', 'rojbpgfm_cy_consultancy');
define('DB_PASS', 'kKJGWrpD])WB');
define('DB_NAME', 'rojbpgfm_cy_consultancy');

function site_db(): mysqli {
    static $c = null;
    if ($c === null) {
        $c = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        if ($c->connect_error) return null;
        $c->set_charset('utf8mb4');
    }
    return $c;
}

function site_setting(string $key, string $default = ''): string {
    $db = site_db();
    if (!$db) return $default;
    $st = $db->prepare("SELECT setting_value FROM site_settings WHERE setting_key=? LIMIT 1");
    $st->bind_param('s', $key);
    $st->execute();
    $r = $st->get_result()->fetch_assoc();
    return $r ? ($r['setting_value'] ?? $default) : $default;
}
