<?php

namespace WPDesk\FakturyKSEF;

/**
 * Manages class aliases to support both Free and PRO versions of the parent plugin.
 *
 * This class detects the active version of the parent plugin (Flexible Invoices)
 * and creates a set of internal aliases for its classes. This allows
 * the rest of the add-on to use a single, consistent set of class names,
 * regardless of which parent plugin version is active.
 */
class RequirementChecker {

	private const PRO_NAMESPACE_PREFIX   = 'WPDeskWCInvoicesVendor\\';
	private const FREE_NAMESPACE_PREFIX  = 'WPDeskFIVendor\\';
	private const ALIAS_NAMESPACE_PREFIX = 'WPDesk\\CoreAlias\\';

	private const MIN_VERSION_PRO  = '5.3.1';
	private const MIN_VERSION_FREE = '6.2.1';

	private const BASENAME_FREE = 'flexible-invoices/flexible-invoices.php';
	private const BASENAME_PRO  = 'flexible-invoices-woocommerce/flexible-invoices-woocommerce.php';

	private array $class_map = [
		'WPDesk\Library\FlexibleInvoicesCore\Decorators\DocumentDecorator' => 'DocumentDecorator',
		'WPDesk\Library\FlexibleInvoicesCore\Helpers\CurrencyHelper' => 'CurrencyHelper',
		'WPDesk\Library\FlexibleInvoicesCore\Integration\DocumentFactory' => 'DocumentFactory',
		'WPDesk\Library\FlexibleInvoicesCore\Integration\MetaPost' => 'MetaPost',
		'WPDesk\Library\FlexibleInvoicesCore\Documents\Invoice' => 'Invoice',
		'WPDesk\Library\FlexibleInvoicesCore\Settings\SettingsForm' => 'SettingsForm',
		'WPDesk\Library\FlexibleInvoicesCore\Settings\Tabs\FieldSettingsTab' => 'FieldSettingsTab',
		'WPDesk\Library\FlexibleInvoicesCore\Settings\Menus\GeneralSettingsMenu' => 'GeneralSettingsMenu',
		'WPDesk\Library\FlexibleInvoicesCore\Settings\Fields\FICheckboxField' => 'FICheckboxField',
		'WPDesk\Library\FlexibleInvoicesCore\Decorators\TemplateDocumentDecorator' => 'TemplateDocumentDecorator',
		'WPDesk\Library\FlexibleInvoicesCore\BlockEditor\EditorBlocks\Replacers\AbstractBasicReplacer' => 'AbstractBasicReplacer',
		'WPDesk\Library\FlexibleInvoicesAbstracts\ValueObjects\DocumentSeller' => 'DocumentSeller',
		'WPDesk\Library\FlexibleInvoicesCore\InvoicesIntegration' => 'InvoicesIntegration',
		'WPDesk\Forms\Field'                => 'Field',
		'WPDesk\Forms\Field\SelectField'    => 'SelectField',
		'WPDesk\Forms\Field\NoOnceField'    => 'NoOnceField',
		'WPDesk\Forms\Field\Header'         => 'Header',
		'WPDesk\Forms\Field\InputTextField' => 'InputTextField',
		'WPDesk\Forms\Field\TextAreaField'  => 'TextAreaField',
		'WPDesk\Forms\Field\SubmitField'    => 'SubmitField',
	];

	private string $active_prefix;

	public function __construct() {
		$this->detect_active_prefix();
	}

	public function is_parent_plugin_active(): bool {
		return isset( $this->active_prefix );
	}

	public function is_parent_plugin_version_compatible(): bool {
		if ( ! $this->is_parent_plugin_active() ) {
			return false;
		}

		$current_version = $this->get_detected_plugin_version();

		$min_required_version = ( $this->active_prefix === self::PRO_NAMESPACE_PREFIX )
			? self::MIN_VERSION_PRO
			: self::MIN_VERSION_FREE;

		return version_compare( $current_version, $min_required_version, '>=' );
	}

	private function get_detected_plugin_version(): string {
		$all_plugins = get_plugins();
		$is_pro      = ( $this->active_prefix === self::PRO_NAMESPACE_PREFIX );

		$standard_path = $is_pro ? self::BASENAME_PRO : self::BASENAME_FREE;
		if ( isset( $all_plugins[ $standard_path ]['Version'] ) ) {
			return $all_plugins[ $standard_path ]['Version'];
		}

		return '0.0.0';
	}

	public function create_aliases(): bool {
		if ( ! $this->is_parent_plugin_active() ) {
			return false;
		}

		if ( ! $this->is_parent_plugin_version_compatible() ) {
			return false;
		}

		foreach ( $this->class_map as $original_class_path => $alias_name ) {
			$source_class = $this->active_prefix . $original_class_path;
			$alias_class  = self::ALIAS_NAMESPACE_PREFIX . $alias_name;

			if ( class_exists( $source_class ) && ! class_exists( $alias_class ) ) {
				class_alias( $source_class, $alias_class );
			}
		}

		return true;
	}

	private function detect_active_prefix(): bool {
		if ( class_exists( 'WPDesk\FlexibleInvoicesPro\Plugin' ) ) {
			$this->active_prefix = self::PRO_NAMESPACE_PREFIX;
			return true;
		}

		if ( class_exists( 'WPDesk\FlexibleInvoices\Plugin' ) ) {
			$this->active_prefix = self::FREE_NAMESPACE_PREFIX;
			return true;
		}

		return false;
	}
}
