<?php

/**
 * Global Helper Functions
 *
 * Provides convenient access to the service container and common services.
 *
 * @package WPUltimateSecurity\UltimateSecurity\Support
 * @since 2.0.0
 */

use WPUltimateSecurity\UltimateSecurity\Bootstrap\Plugin;
use WPUltimateSecurity\UltimateSecurity\Infrastructure\Container\Container;
use WPUltimateSecurity\UltimateSecurity\Infrastructure\Settings\SettingsInterface;

if (!function_exists('ultimate_security_container')) {
    /**
     * Get the service container instance.
     *
     * @return Container
     */
    function ultimate_security_container(): Container
    {
        return Plugin::container();
    }
}

if (!function_exists('ultimate_security_resolve')) {
    /**
     * Resolve a service from the container.
     *
     * @param string $id      The service identifier.
     * @param mixed  $default Default value if service not found (optional).
     *
     * @return mixed The resolved service or default.
     */
    function ultimate_security_resolve(string $id, mixed $default = null): mixed
    {
        $container = ultimate_security_container();

        if (!$container->has($id)) {
            return $default;
        }

        return $container->get($id);
    }
}

if (!function_exists('ultimate_security_settings')) {
    /**
     * Get the settings repository instance.
     *
     * @return SettingsInterface
     */
    function ultimate_security_settings(): SettingsInterface
    {
        return ultimate_security_container()->get(SettingsInterface::class);
    }
}

if (!function_exists('ultimate_security_setting')) {
    /**
     * Get a specific setting value.
     *
     * @param string $key     The setting key (supports dot notation).
     * @param mixed  $default Default value if not found.
     *
     * @return mixed The setting value.
     */
    function ultimate_security_setting(string $key, mixed $default = null): mixed
    {
        return ultimate_security_settings()->get($key, $default);
    }
}
