<?php
// admin/embed_bootstrap.php
// Used by pages that run in embed mode (embed=1) without header.php.
// Ensures DB connection exists and exposes $conn in local scope.

if (session_status() === PHP_SESSION_NONE) {
  @session_start();
}

require_once __DIR__ . '/db_bootstrap.php';

$conn = $GLOBALS['conn'] ?? null;
if (!($conn instanceof mysqli)) {
  $conn = @ensure_db(false);
  if ($conn instanceof mysqli) {
    $GLOBALS['conn'] = $conn;
  }
}

require_once __DIR__ . '/settings_store.php';
require_once __DIR__ . '/lang.php';

if (!function_exists('get_app_setting')) {
  function get_app_setting(mysqli $conn, string $k, string $default=''): string {
    $v = function_exists('setting_get') ? setting_get($conn, $k, $default) : $default;
    return is_array($v) ? $default : (string)$v;
  }
}
if (!function_exists('set_app_setting')) {
  function set_app_setting(mysqli $conn, string $k, $v): bool {
    return function_exists('setting_set') ? setting_set($conn, $k, $v) : false;
  }
}


// Visual layer helper for embedded pages that intentionally skip header.php/footer.php.
// It prints only after the page finishes authentication, so redirects stay safe.
if (!function_exists('ua_embed_glass_asset')) {
  function ua_embed_glass_asset(): void {
    static $printed = false;
    if ($printed) return;
    $printed = true;
    $ua_glass_v = @filemtime(__DIR__ . '/assets/css/ua_global_glass_system.css') ?: time();
    echo "\n<link rel=\"stylesheet\" href=\"assets/css/ua_global_glass_system.css?v=" . (int)$ua_glass_v . "\" data-ua-glass-system-embed>\n";
  }
}
