<?php

namespace WPUltimateSecurity\UltimateSecurity\Bootstrap;

use WPUltimateSecurity\UltimateSecurity\Bootstrap\Providers\AdminServiceProvider;
use WPUltimateSecurity\UltimateSecurity\Bootstrap\Providers\ApiServiceProvider;
use WPUltimateSecurity\UltimateSecurity\Bootstrap\Providers\ContainerServiceProvider;
use WPUltimateSecurity\UltimateSecurity\Bootstrap\Providers\HooksServiceProvider;
use WPUltimateSecurity\UltimateSecurity\Bootstrap\Providers\LifecycleServiceProvider;
use WPUltimateSecurity\UltimateSecurity\Bootstrap\Providers\ModuleServiceProvider;
use WPUltimateSecurity\UltimateSecurity\Bootstrap\Providers\PublicServiceProvider;
use WPUltimateSecurity\UltimateSecurity\Contracts\ServiceProviderInterface;
use WPUltimateSecurity\UltimateSecurity\Infrastructure\Container\Container;

class Plugin
{
    /**
     * The container instance.
     *
     * @var Container|null
     */
    private static ?Container $container = null;

    /**
     * Boot the plugin by running all service providers.
     */
    public static function init(): void
    {
        // Initialize container first
        self::$container = Container::getInstance();

        foreach (self::providers() as $provider) {
            $provider->register();
        }

        // Fire action after all providers are registered
        do_action('ultimate_security_initialized', self::$container);
    }

    /**
     * Get the container instance.
     *
     * @return Container
     */
    public static function container(): Container
    {
        if (self::$container === null) {
            self::$container = Container::getInstance();
        }

        return self::$container;
    }

    /**
     * @return ServiceProviderInterface[]
     */
    private static function providers(): array
    {
        return [
            new ContainerServiceProvider(),  // Container services first
            new LifecycleServiceProvider(),
            new HooksServiceProvider(),
            new AdminServiceProvider(),
            new ApiServiceProvider(),
            new PublicServiceProvider(),
            new ModuleServiceProvider(),
        ];
    }
}
