<?php
/**
 * Core Plugin Class
 *
 * @package SEOvaultSEO
 */

namespace SEOvaultSEO\Core;

/**
 * Main plugin class
 *
 * @internal Free bootstrap singleton. Extensions must not depend on Plugin
 *           internals or Container services. Use Extension\ExtensionApi hooks
 *           and documented public classes instead.
 */
class Plugin {

	/**
	 * Single instance of the plugin
	 *
	 * @var Plugin|null
	 */
	private static $instance = null;

	/**
	 * Container instance
	 *
	 * @var Container
	 */
	private $container;

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

		return self::$instance;
	}

	/**
	 * Constructor
	 */
	private function __construct() {
		$this->container = new Container();
	}

	/**
	 * Initialize the plugin
	 */
	public function init() {
		// WP 6.7+: load translations on init, not plugins_loaded.
		if ( did_action( 'init' ) ) {
			I18n::load_textdomain();
		} else {
			add_action( 'init', array( I18n::class, 'load_textdomain' ), 0 );
		}

		// Versioned Free ↔ Pro extension contract (hooks, shared assets, capabilities).
		\SEOvaultSEO\Extension\ExtensionApi::init();

		// Schema / version upgrades for installs that update without re-activating.
		Upgrade::maybe_upgrade();

		// P7-010: one-time autoload=no migration for existing installs (no re-activation required).
		Activator::maybe_disable_option_autoload();

		// Initialize replace variables manager
		\SEOvaultSEO\ReplaceVariables\Manager::get_instance();

		// Initialize frontend output
		\SEOvaultSEO\Frontend\Head::init();
		\SEOvaultSEO\Frontend\Title::init();
		\SEOvaultSEO\Frontend\Redirect::init();
		// Priority 1 fallback runs after Redirect Manager's priority 0 handler.
		\SEOvaultSEO\NotFound\Bootstrap::init();
		\SEOvaultSEO\Frontend\QueryExclusion::init();
		\SEOvaultSEO\Frontend\CommentSeo::init();

		// Initialize 404 Monitor (frontend logging only when enabled)
		if ( \SEOvaultSEO\Core\Options::is_monitor404_enabled() ) {
			\SEOvaultSEO\Monitor404\Monitor::init();
		}

		// Initialize breadcrumbs
		\SEOvaultSEO\Frontend\Breadcrumbs::init();

		// Initialize Image SEO (auto ALT/title attributes)
		\SEOvaultSEO\Frontend\ImageSeo::init();

		// Initialize Frontend SEO Score display
		\SEOvaultSEO\Frontend\FrontendScore::init();

		// Guard FE SEO UI / editor-adjacent scripts on WC cart & checkout blocks.
		\SEOvaultSEO\Utils\FrontendSeoEnqueue::init();

		// Initialize Analytics tracking code injection
		\SEOvaultSEO\Frontend\TrackingCode::init();

		// Initialize feed optimizations
		\SEOvaultSEO\Frontend\Feed::init();

		// Initialize oEmbed improvements
		\SEOvaultSEO\Frontend\OEmbed::init();

		// Initialize REST API
		\SEOvaultSEO\Api\Router::init();

		// Initialize AI Visibility features
		\SEOvaultSEO\AiVisibility\Llms::init();
		\SEOvaultSEO\AiVisibility\Robots::init();
		\SEOvaultSEO\AiVisibility\CrawlerLogs::init();

		// Initialize sitemap manager
		\SEOvaultSEO\Sitemap\Manager::init();

		// Initialize IndexNow
		\SEOvaultSEO\IndexNow\IndexNowManager::get_instance()->init();

		// Initialize schema manager
		\SEOvaultSEO\Schema\SchemaManager::init();

		// Initialize Gutenberg blocks (FAQ contract + FAQPage collection in SchemaManager).
		\SEOvaultSEO\Blocks\Bootstrap::init();

		// Initialize Local SEO module
		\SEOvaultSEO\LocalSEO\LocalSEOInit::init();

		// Initialize WooCommerce integration if WC is active
		\SEOvaultSEO\WooCommerce\WooCommerce::init();

		// Meta box + AJAX handlers. Must run on every request — admin-ajax.php
		// never fires admin_menu, so hooks cannot be registered there only.
		\SEOvaultSEO\Admin\MetaBox::init();

		// Elementor editor integration + protected metadata save endpoint.
		\SEOvaultSEO\Integrations\Elementor\Bootstrap::init();

		// Initialize migration manager (admin only AJAX)
		if ( is_admin() ) {
			\SEOvaultSEO\Migration\MigrationManager::init();
		}

		// Initialize admin if in admin area
		if ( is_admin() ) {
			\SEOvaultSEO\Admin\Dashboard::init();
			\SEOvaultSEO\Admin\Onboarding::init();
			\SEOvaultSEO\Admin\TermMetaBox::init();
			\SEOvaultSEO\Admin\SeoBar::init();
			\SEOvaultSEO\Admin\QuickEdit::init();
			\SEOvaultSEO\Admin\Privacy::init();
			\SEOvaultSEO\RedirectManager\AdminPage::init();
			\SEOvaultSEO\SeoAnalyzer\AdminPage::init();
			\SEOvaultSEO\Admin\Notices::init();
			\SEOvaultSEO\Admin\SchemaMigrationAdmin::init();
			\SEOvaultSEO\Admin\SchemaSettingsAdmin::init();
			\SEOvaultSEO\Schema\SchemaOnboardingGuidance::init();
			add_action( 'admin_init', array( __CLASS__, 'register_settings' ) );
			add_action( 'admin_init', array( __CLASS__, 'handle_admin_actions' ) );
			add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_settings_assets' ) );
			add_action( 'wp_ajax_seovaultseo_robots_preview', array( '\SEOvaultSEO\Admin\SettingsPage', 'ajax_robots_preview' ) );
			add_action( 'wp_ajax_seovaultseo_log_save_all_settings_click', array( '\SEOvaultSEO\Admin\SettingsPage', 'ajax_log_save_all_settings_click' ) );
		}
	}

	/**
	 * Register settings (delegates to SettingsPage for backward compat)
	 */
	public static function register_settings() {
		if ( class_exists( '\SEOvaultSEO\Admin\SettingsPage' ) ) {
			\SEOvaultSEO\Admin\SettingsPage::register_settings();
		}
	}

	/**
	 * Handle admin actions (delegates to SettingsPage for backward compat)
	 */
	public static function handle_admin_actions() {
		if ( class_exists( '\SEOvaultSEO\Admin\SettingsPage' ) ) {
			\SEOvaultSEO\Admin\SettingsPage::handle_admin_actions();
		}
	}

	/**
	 * Enqueue settings page assets (delegates to SettingsPage)
	 */
	public static function enqueue_settings_assets( $hook ) {
		if ( class_exists( '\SEOvaultSEO\Admin\SettingsPage' ) ) {
			\SEOvaultSEO\Admin\SettingsPage::enqueue_assets( $hook );
		}
	}

	/**
	 * Get container
	 *
	 * @internal Not part of the public extension API. Pro must not resolve Free
	 *           services through this container.
	 *
	 * @return Container
	 */
	public function get_container() {
		return $this->container;
	}
}
