<?php
/**
 * Main plugin coordinator.
 *
 * @package CertaCart_EU_Product_Readiness
 */

namespace CertaCart\EU_Product_Readiness;

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

final class Plugin {
	private static ?Plugin $instance = null;
	private bool $booted = false;

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

	private function __construct() {}

	public function boot(): void {
		if ( $this->booted ) {
			return;
		}
		$this->booted = true;

		if ( ! class_exists( 'WooCommerce' ) ) {
			add_action( 'admin_notices', array( $this, 'render_missing_woocommerce_notice' ) );
			return;
		}

		$this->maybe_upgrade_schema();
		Settings::instance()->register();
		Product_Fields::instance()->register();
		Storefront_Renderer::instance()->register();
		Admin::instance()->register();
		Rest_Controller::instance()->register();
		Privacy::register();
		Blocks::register();
		Cleanup::register();
		Commercial::register();

		if ( defined( 'WP_CLI' ) && WP_CLI ) {
			Cli_Command::register();
		}

		do_action( 'certacart_product_readiness_loaded', $this );
	}

	private function maybe_upgrade_schema(): void {
		if ( Schema::VERSION !== get_option( 'ccpr_schema_version' ) ) {
			Schema::install();
		}
	}

	public function render_missing_woocommerce_notice(): void {
		if ( ! current_user_can( 'activate_plugins' ) ) {
			return;
		}
		?>
		<div class="notice notice-error"><p><?php esc_html_e( 'CertaCart EU Product Readiness requires WooCommerce to be installed and active.', 'certacart-eu-product-readiness' ); ?></p></div>
		<?php
	}
}
