<?php
namespace NeoMotionBeta\NeoGlobal; if (!defined("ABSPATH")) { die("neoWP direct access not allowed"); }




\NeoMotionBeta\NeoGlobal\val("neo_global__suppress_log", false);
function suppress_log($suppress = true) { \NeoMotionBeta\NeoGlobal\val("neo_global__suppress_log", $suppress); }


function global_log($message, $log_source = null) {
    if (\NeoMotionBeta\NeoGlobal\val("neo_global__suppress_log")) { return; }
    if (!\NeoMotionBeta\NeoGlobal\is_interface_system_available()) { \NeoMotionBeta\NeoGlobal\add_action_hook("neo_init", fn () => global_log($message, $log_source)); return; }
    \NeoMotionBeta\NeoGlobal\call_interface_func_implemented('\NeoMotionBeta\NeoLog\interface_global_log_20250702')($message, $log_source);
}
function global_log_with_module_name($module_name_or_global_filename_base, $message, $log_source = null) {
    global_log($message, log_source: $log_source ?? $module_name_or_global_filename_base);
}


function global_warn($message, $log_source = null) {
    if (\NeoMotionBeta\NeoGlobal\val("neo_global__suppress_log")) { return; }
    if (!\NeoMotionBeta\NeoGlobal\is_interface_system_available()) {
        \NeoMotionBeta\NeoGlobal\add_action_hook("neo_init", fn () => global_warn("[⚠️ Stack Trace lost because of delayed warning - set breakpoint in _global--log.php to see stack trace] " . $message, $log_source));
        return;
    }
    [$_, $interface_ok] = \NeoMotionBeta\NeoGlobal\call_interface_func_implemented('\NeoMotionBeta\NeoLog\interface_global_warn_20250325')($message, $log_source);
    if (!$interface_ok) {
        error_log("global_warn: " . $message); /* phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log */ // Error log is not for debugging here. It is only a fallback that's useful to the user if the neoLog-internal interface is not available because of an error.
    }
}
function global_warn_with_module_name($module_name_or_global_filename_base, $message, $log_source = null) {
    global_warn($message, log_source: $log_source ?? $module_name_or_global_filename_base);
}





class GlobalException extends \Exception {
    private $status_code;

    
    public function __construct($message, $status_code = 500) {
        if (\NeoMotionBeta\NeoGlobal\is_interface_system_available()) { [$additional_message_for_errors, $interface_ok] = \NeoMotionBeta\NeoGlobal\call_interface_func_implemented('\NeoMotionBeta\NeoLog\interface_additional_message_for_errors_20250505')(); } else { $interface_ok = false; }
        $message = esc_html($message); // Escape error message to avoid security vulnerabilities
        parent::__construct($message . ($interface_ok ? (" - " . $additional_message_for_errors) : ""), 0, null);
        $this->status_code = $status_code;
        
        global_log("$message in "        . $this->getTraceAsString());
        global_log("Error: $message in " . $this->getTraceAsString(), log_source: "error");
    }


    public function get_status_code() { return $this->status_code; }
}

function throw_global_exception($message, $status_code = 500) {
    throw new \NeoMotionBeta\NeoGlobal\GlobalException($message, $status_code); /* phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped */ /* Our custom GlobalException class above already escapes all inputs properly. */
}


function admin_info($message, $title = null) {
    if (\NeoMotionBeta\NeoGlobal\val("neo_global__suppress_log")) { return; }
    if (!\NeoMotionBeta\NeoGlobal\is_interface_system_available()) { \NeoMotionBeta\NeoGlobal\add_action_hook("neo_init", fn () => admin_info($message, $title)); return; }
    \NeoMotionBeta\NeoGlobal\call_interface_func_implemented('\NeoMotionBeta\NeoLog\interface_admin_info_20251129')($message, $title);
}
function admin_success($message, $title = null) {
    if (\NeoMotionBeta\NeoGlobal\val("neo_global__suppress_log")) { return; }
    if (!\NeoMotionBeta\NeoGlobal\is_interface_system_available()) { \NeoMotionBeta\NeoGlobal\add_action_hook("neo_init", fn () => admin_success($message, $title)); return; }
    \NeoMotionBeta\NeoGlobal\call_interface_func_implemented('\NeoMotionBeta\NeoLog\interface_admin_success_20251129')($message, $title);
}

function admin_warn($message, $title = null) {
    if (\NeoMotionBeta\NeoGlobal\val("neo_global__suppress_log")) { return; }
    if (!\NeoMotionBeta\NeoGlobal\is_interface_system_available()) { \NeoMotionBeta\NeoGlobal\add_action_hook("neo_init", fn () => admin_warn($message, $title)); return; }
    \NeoMotionBeta\NeoGlobal\call_interface_func_implemented('\NeoMotionBeta\NeoLog\interface_admin_warn_20251129')($message, $title);
}

function admin_error($message, $title = null) {
    if (\NeoMotionBeta\NeoGlobal\val("neo_global__suppress_log")) { return; }
    if (!\NeoMotionBeta\NeoGlobal\is_interface_system_available()) { \NeoMotionBeta\NeoGlobal\add_action_hook("neo_init", fn () => admin_error($message, $title)); return; }
    \NeoMotionBeta\NeoGlobal\call_interface_func_implemented('\NeoMotionBeta\NeoLog\interface_admin_error_20251129')($message, $title);
}
