<?php
/**
 * Plugin bootstrap — wires layers without coupling Core to React.
 *
 * @package AuraBuilder
 */

declare(strict_types=1);

namespace AuraBuilder;

use AuraBuilder\Admin\EditorPage;
use AuraBuilder\Admin\HomePageBridge;
use AuraBuilder\Admin\PagesList;
use AuraBuilder\Admin\PolicyPage;
use AuraBuilder\Admin\SettingsPage;
use AuraBuilder\Admin\TemplatesPage;
use AuraBuilder\Admin\ThemeBuilderPage;
use AuraBuilder\Core\EventBus;
use AuraBuilder\Frontend\BrandCredit;
use AuraBuilder\Frontend\ConstructionMode;
use AuraBuilder\Frontend\ContentFilter;
use AuraBuilder\Globals\SiteGlobals;
use AuraBuilder\Licensing\License;
use AuraBuilder\REST\RestRegistrar;
use AuraBuilder\Theme\ThemeBuilder;
use AuraBuilder\Widgets\WidgetBootstrap;

final class Plugin {

	private static ?self $instance = null;

	private EventBus $events;

	public static function instance(): self {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		$this->events = new EventBus();
	}

	public function events(): EventBus {
		return $this->events;
	}

	public function boot(): void {
		// Ensure admin/editor roles keep builder capabilities after updates.
		add_action( 'admin_init', array( \AuraBuilder\Support\Capabilities::class, 'ensure_admin_caps' ) );

		WidgetBootstrap::register_defaults();
		( new RestRegistrar() )->hooks();
		// PagesList first (priority 5) registers top-level menu slug aura-builder-pages.
		( new PagesList() )->hooks();
		( new EditorPage() )->hooks();
		( new SettingsPage() )->hooks();
		( new PolicyPage() )->hooks();
		// Force https:// for plugin CSS/JS when the public site is SSL (mixed-content fix).
		\AuraBuilder\Support\Assets::force_https_assets();
		( new ThemeBuilderPage() )->hooks();
		( new TemplatesPage() )->hooks();
		( new HomePageBridge() )->hooks();
		( new ContentFilter() )->hooks();
		( new ConstructionMode() )->hooks();
		BrandCredit::hooks();
		License::hooks();
		SiteGlobals::hooks();
		ThemeBuilder::hooks();

		$this->events->emit( 'plugin.ready', array( 'version' => AURA_BUILDER_VERSION ) );
	}
}
