<?php

declare(strict_types=1);

namespace Signocore\Multilanguage\Application;

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

use Signocore\Multilanguage\Admin\Dashboard;
use Signocore\Multilanguage\Admin\GuidePage;
use Signocore\Multilanguage\Admin\Settings;
use Signocore\Multilanguage\Admin\SetupWizard;
use Signocore\Multilanguage\Admin\StringTranslationPage;
use Signocore\Multilanguage\Features\ContentDuplicator;
use Signocore\Multilanguage\Features\HreflangTags;
use Signocore\Multilanguage\Features\LanguageDetection;
use Signocore\Multilanguage\Features\LanguageManager;
use Signocore\Multilanguage\Features\LanguageSwitcher;
use Signocore\Multilanguage\Features\MediaTranslation;
use Signocore\Multilanguage\Features\MenuSwitcherInjection;
use Signocore\Multilanguage\Features\MenuTranslation;
use Signocore\Multilanguage\Features\OptionsTranslation;
use Signocore\Multilanguage\Features\PostTranslation;
use Signocore\Multilanguage\Features\RtlSupport;
use Signocore\Multilanguage\Features\SharedSlugs;
use Signocore\Multilanguage\Features\StringScanner;
use Signocore\Multilanguage\Features\StringTranslation;
use Signocore\Multilanguage\Features\StringTranslationHooks;
use Signocore\Multilanguage\Features\TaxonomyTranslation;
use Signocore\Multilanguage\Features\PermalinkTranslation;
use Signocore\Multilanguage\Features\UrlRewriting;
use Signocore\Multilanguage\Infrastructure\Compatibility\Elementor;
use Signocore\Multilanguage\Infrastructure\Compatibility\RankMath;
use Signocore\Multilanguage\Infrastructure\Compatibility\SignocoreSEO;
use Signocore\Multilanguage\Infrastructure\Compatibility\WooCommerce;
use Signocore\Multilanguage\Infrastructure\Compatibility\Yoast;
use Signocore\Multilanguage\Infrastructure\Hooks\PublicAPI;
use Signocore\Multilanguage\UI\AdminColumns;
use Signocore\Multilanguage\UI\Blocks\LanguageSwitcher as LanguageSwitcherBlock;
use Signocore\Multilanguage\UI\Metaboxes\PostTranslation as PostTranslationMetabox;
use Signocore\Multilanguage\UI\Metaboxes\TaxonomyTranslation as TaxonomyTranslationMetabox;
use Signocore\Multilanguage\UI\Widgets\LanguageSwitcher as LanguageSwitcherWidget;

final class ServiceRegistry
{
	public static function boot(): void
	{
		$license = LicenseManager::load();

		// Run any pending DB migrations before services query the schema.
		Migrator::load()->migrate();

		self::initAlways($license);
		self::initAdmin($license);
		self::initFrontend($license);
		self::initCompatibility($license);
	}

	private static function initAlways(LicenseManager $license): void
	{
		// Core language services
		LanguageManager::load();
		LanguageDetection::load();
		UrlRewriting::load();
		PermalinkTranslation::load();
		StringTranslation::load();
		StringTranslationHooks::load();
		StringScanner::load();

		// Translation features
		PostTranslation::load();
		TaxonomyTranslation::load();
		MenuTranslation::load();
		OptionsTranslation::load();

		// Pro features (license-gated internally)
		MediaTranslation::load();
		ContentDuplicator::load();

		if ($license->can(Entitlements::SHARED_SLUGS)) {
			SharedSlugs::load();
		}

		// Public API template functions
		PublicAPI::load();
	}

	private static function initAdmin(LicenseManager $license): void
	{
		if (!Constants::$isAdmin) {
			return;
		}

		Database::load();
		Updater::load();
		Ajax::load();

		Settings::load();
		Dashboard::load();
		SetupWizard::load();
		StringTranslationPage::load();
		GuidePage::load();

		// Admin UI components
		AdminColumns::load();
		PostTranslationMetabox::load();
		TaxonomyTranslationMetabox::load();
	}

	private static function initFrontend(LicenseManager $license): void
	{
		// Block must be registered on all requests (admin needs it for the editor)
		LanguageSwitcherBlock::load();

		// Register classic widget on all requests (admin needs it for the widget editor)
		add_action('widgets_init', static function (): void {
			register_widget(LanguageSwitcherWidget::class);
		});

		if (Constants::$isAdmin) {
			return;
		}

		// Frontend-only features
		LanguageSwitcher::load();
		MenuSwitcherInjection::load();
		HreflangTags::load();
		RtlSupport::load();
	}

	private static function initCompatibility(LicenseManager $license): void
	{
		// Always load Signocore SEO integration (free)
		SignocoreSEO::load();

		// Pro compatibility modules
		if ($license->can(Entitlements::WOOCOMMERCE)) {
			WooCommerce::load();
		}

		if ($license->can(Entitlements::ELEMENTOR)) {
			Elementor::load();
		}

		if ($license->can(Entitlements::YOAST)) {
			Yoast::load();
		}

		if ($license->can(Entitlements::RANKMATH)) {
			RankMath::load();
		}
	}
}
