<?php
/**
 * Main Theme Bootstrapper — delegates to ModuleManager.
 *
 * @package Kutby
 * @since   1.0.0
 */

declare(strict_types=1);

namespace Kutby\Core;

use Kutby\Events\Dispatcher;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Theme
 */
final class Theme {

	/**
	 * @var self|null
	 */
	private static ?self $instance = null;

	/**
	 * @var bool
	 */
	private bool $booted = false;

	/**
	 * @var ModuleManager|null
	 */
	private ?ModuleManager $modules = null;

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

		return self::$instance;
	}

	private function __construct() {}

	private function __clone() {}

	/**
	 * Boot theme via Module Manager only.
	 *
	 * @return void
	 */
	public function boot(): void {
		if ( $this->booted ) {
			return;
		}

		$this->booted  = true;
		$this->modules = new ModuleManager();
		$this->modules->run();

		Dispatcher::emit( 'theme_booted' );

		/**
		 * Fires after Kutby has fully booted (BC).
		 *
		 * @since 1.0.0
		 */
		do_action( 'kutby_booted' );
	}

	/**
	 * Access the module manager after boot.
	 *
	 * @return ModuleManager|null
	 */
	public function modules(): ?ModuleManager {
		return $this->modules;
	}
}
