<?php
/**
 * Main plugin bootstrap.
 *
 * @package PhoenixWP\PriceLabels
 */

declare(strict_types=1);

namespace PhoenixWP\PriceLabels;

use PhoenixWP\Core\Module_Registry;
use PhoenixWP\PriceLabels\Admin\Labels_Admin;

defined( 'ABSPATH' ) || exit;

/**
 * Extension singleton bootstrap.
 */
final class Plugin {

	private static ?self $instance = null;

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

		return self::$instance;
	}

	private function __construct() {
		add_action( 'before_woocommerce_init', array( $this, 'declare_woocommerce_compatibility' ) );
		add_action( 'phoenix_wp_core_register_modules', array( $this, 'register_module' ) );
		add_action( 'plugins_loaded', array( $this, 'init' ), 20 );
		add_action( 'admin_notices', array( $this, 'dependency_notices' ) );
	}

	private function __clone() {}

	/**
	 * Declares HPOS and Cart/Checkout Blocks compatibility.
	 */
	public function declare_woocommerce_compatibility(): void {
		if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
				'custom_order_tables',
				PHOENIX_PRICE_LABELS_FOR_WOOCOMMERCE_FILE,
				true
			);
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
				'cart_checkout_blocks',
				PHOENIX_PRICE_LABELS_FOR_WOOCOMMERCE_FILE,
				true
			);
		}
	}

	public function __wakeup(): void {
		throw new \Exception( 'Cannot unserialize singleton.' );
	}

	/**
	 * Initializes the extension after dependencies load.
	 */
	public function init(): void {
		if ( is_admin() ) {
			Labels_Admin::instance()->init();
		}

		if ( ! Install::requirements_met() ) {
			return;
		}

		/**
		 * Fires when Price Labels is fully loaded.
		 */
		do_action( 'phoenix_wp_price_labels_loaded' );
	}

	/**
	 * Registers module metadata with PhoenixWP Core.
	 *
	 * @param Module_Registry $registry Core registry.
	 */
	public function register_module( Module_Registry $registry ): void {
		$registry->register(
			array(
				'slug'    => 'phoenix-price-labels-for-woocommerce',
				'name'    => phoenix_wp_price_labels_get_plugin_display_name(),
				'version' => PHOENIX_PRICE_LABELS_FOR_WOOCOMMERCE_VERSION,
				'type'    => Module_Registry::TYPE_EXTENSION,
				'tier'    => 'free',
				'file'    => PHOENIX_PRICE_LABELS_FOR_WOOCOMMERCE_FILE,
			)
		);
	}

	/**
	 * Shows soft dependency notices in admin.
	 */
	public function dependency_notices(): void {
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			return;
		}

		if ( ! Install::requirements_met() ) {
			echo '<div class="notice notice-error"><p>';
			echo esc_html__(
				'PhoenixWP Price Labels requires WooCommerce.',
				'phoenix-price-labels-for-woocommerce'
			);
			echo '</p></div>';
		}

		if ( ! is_readable( PHOENIX_PRICE_LABELS_FOR_WOOCOMMERCE_PATH . 'vendor/autoload.php' ) ) {
			echo '<div class="notice notice-error"><p>';
			echo esc_html__(
				'PhoenixWP Price Labels: Run composer install in the plugin directory (Dompdf is required).',
				'phoenix-price-labels-for-woocommerce'
			);
			echo '</p></div>';
		}
	}
}
