<?php
// admin/app_paths.php
// Stage 7: central path/session/json primitives shared by bootstrap, helpers and API endpoints.
// This file must stay side-effect light: no DB connection, no auth redirect, no HTML output.

// Keep every admin page, API endpoint and worker on the same clock as the
// database and RouterOS. Without this bootstrap PHP inherited Europe/Berlin
// while the installation runs in Asia/Damascus, making future logout times
// look like "now" in subscriber activity labels.
$uaConstantsFile = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'constants.php';
if (is_file($uaConstantsFile)) require_once $uaConstantsFile;

if (!defined('UA_ADMIN_DIR')) {
  define('UA_ADMIN_DIR', __DIR__);
}
if (!defined('UA_PROJECT_ROOT')) {
  define('UA_PROJECT_ROOT', dirname(__DIR__));
}

if (!function_exists('ua_admin_path')) {
  function ua_admin_path(string $relative = ''): string {
    $relative = ltrim($relative, '/\\');
    return UA_ADMIN_DIR . ($relative !== '' ? DIRECTORY_SEPARATOR . $relative : '');
  }
}

if (!function_exists('ua_project_path')) {
  function ua_project_path(string $relative = ''): string {
    $relative = ltrim($relative, '/\\');
    return UA_PROJECT_ROOT . ($relative !== '' ? DIRECTORY_SEPARATOR . $relative : '');
  }
}

if (!function_exists('ua_is_logged_in')) {
  function ua_is_logged_in(): bool {
    return !empty($_SESSION['admin_id']) || !empty($_SESSION['admin_user_id']) || !empty($_SESSION['admin_user']);
  }
}

if (!function_exists('ua_json_flags')) {
  function ua_json_flags(): int {
    return JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
  }
}
