<?php
defined( 'ABSPATH' ) || exit;

/**
 * Main plugin loader — singleton bootstrap.
 */
class TFDC_Loader {

	/** @var self|null */
	private static $instance = null;

	/**
	 * Get the single instance.
	 *
	 * @return self
	 */
	public static function get_instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Constructor — runs once.
	 */
	private function __construct() {
		if ( ! class_exists( 'WooCommerce' ) ) {
			add_action( 'admin_notices', array( $this, 'wc_missing_notice' ) );
			return;
		}

		$this->load_files();
		$this->init_hooks();
	}

	/**
	 * Require all plugin files.
	 */
	private function load_files() {
		// Includes.
		require_once TFDC_DIR . 'includes/class-tfdc-shortcode.php';
		require_once TFDC_DIR . 'includes/class-tfdc-admin.php';
		require_once TFDC_DIR . 'includes/class-tfdc-buy-now.php';
		require_once TFDC_DIR . 'includes/class-tfdc-product-settings.php';
		require_once TFDC_DIR . 'includes/class-tfdc-landing-checkout.php';
		require_once TFDC_DIR . 'includes/class-tfdc-template-library.php';

		// REST API.
		require_once TFDC_DIR . 'includes/api/class-tfdc-endpoint-base.php';
		require_once TFDC_DIR . 'includes/api/class-tfdc-endpoint-settings.php';
		require_once TFDC_DIR . 'includes/api/class-tfdc-endpoint-landing-pages.php';
		require_once TFDC_DIR . 'includes/api/class-tfdc-api.php';
	}

	/**
	 * Register hooks.
	 */
	private function init_hooks() {
		// Shortcode.
		TFDC_Shortcode::get_instance();

		// REST API.
		TFDC_Api::get_instance();

		// Native checkout landing pages.
		TFDC_Landing_Checkout::get_instance();

		// Admin settings and AJAX handlers.
		if ( is_admin() || wp_doing_ajax() ) {
			TFDC_Admin::get_instance();
		}

		// Product-level settings.
		TFDC_Product_Settings::get_instance();

		// Buy Now buttons.
		TFDC_Buy_Now::get_instance();
	}

	/**
	 * Admin notice when WooCommerce is not active.
	 */
	public function wc_missing_notice() {
		?>
		<div class="notice notice-error">
			<p>
				<strong><?php esc_html_e( 'TukiFlow', 'tukiflow-direct-checkout-woocommerce' ); ?></strong>
				<?php esc_html_e( 'requires WooCommerce to be installed and active.', 'tukiflow-direct-checkout-woocommerce' ); ?>
			</p>
		</div>
		<?php
	}
}
