<?php
/**
 * Factory bootstrap for Smart Categories for WooCommerce. The ONLY place that
 * wires the shared core to this plugin — Strauss rewrites these core references
 * at build time to the plugin-prefixed Vendor namespace.
 *
 * The product itself still lives in includes/ (the SCWC_* free classes, plus the
 * premium hierarchy engine in the pro build): the live wp-cli + Chrome suites
 * proved that engine, so the factory migration changes the wiring, not the engine.
 *
 * @package ErenLabs\SmartCategories
 */

namespace ErenLabs\SmartCategories;

use ErenLabs\SmartCategories\Vendor\Hakki\WooCore\Core;

defined( 'ABSPATH' ) || exit;

final class Bootstrap {

	private const SLUG = 'smart-categories-for-woocommerce';

	/** @var Core|null */
	private static $core = null;

	/**
	 * Build (or fetch) the shared core for this plugin. Core::boot() also
	 * declares HPOS + checkout-blocks compatibility.
	 */
	public static function core(): Core {
		if ( null === self::$core ) {
			self::$core = Core::boot(
				array(
					'slug'         => self::SLUG,
					'file'         => defined( 'SCWC_FILE' ) ? SCWC_FILE : __FILE__,
					'version'      => defined( 'SCWC_VERSION' ) ? SCWC_VERSION : '0.0.0',
					'group'        => 'b2c',
					'requires_woo' => true,
					'min_woo'      => '7.0',
					// Freemius later supplies the 'can' resolver (licence check).
				)
			);
		}
		return self::$core;
	}

	/**
	 * Free-yields-to-pro guard (Core::should_boot): when the pro artifact is
	 * co-installed it defines SMART_CATEGORIES_FOR_WOOCOMMERCE_PRO_ACTIVE with its
	 * own file path, and any OTHER instance of this plugin steps aside.
	 *
	 * Self-recognition runs HERE, before delegating to the shared core: in a raw
	 * dev tree the unprefixed Hakki\WooCore can be provided by a co-installed
	 * factory plugin carrying an OLDER core whose should_boot() lacks the
	 * value-equals-own-file exception — which would make the pro build yield to
	 * itself. Strauss-built artifacts never hit this (each plugin gets its own
	 * prefixed core), but the guard must be correct in every tree.
	 */
	public static function should_boot(): bool {
		$pro_const = 'SMART_CATEGORIES_FOR_WOOCOMMERCE_PRO_ACTIVE';
		if ( defined( $pro_const ) && defined( 'SCWC_FILE' ) && constant( $pro_const ) === SCWC_FILE ) {
			return true; // The pro marker is OURS — never yield to ourselves.
		}
		return self::core()->should_boot();
	}
}
