<?php
/**
 * Plugin Name:       FlexiLay Gateway for WooCommerce
 * Plugin URI:        https://flexilay.com/woocommerce/
 * Description:       FlexiLay lay-buy payment gateway for WooCommerce. Creates a FlexiLay plan and redirects the customer to set up their flexible lay-buy schedule.
 * Author:            FlexiLay Pty Ltd
 * Author URI:        https://flexilay.com/
 * Version:           1.3.1
 * Text Domain:       flexilay-gateway-for-woocommerce
 * License:           GPLv2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Requires at least: 6.0
 * Requires PHP:      7.4
 * Requires Plugins:  woocommerce
 * WC requires at least: 8.0
 * WC tested up to:   10.6.2
 */

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

define( 'FLEXILAY_WOO_PLUGIN_VERSION', '1.3.1' );
define( 'FLEXILAY_WOO_PLUGIN_FILE', __FILE__ );
define( 'FLEXILAY_WOO_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'FLEXILAY_WOO_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

add_action( 'before_woocommerce_init', function () {
	if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
	}
} );

// Core (safe even if WooCommerce isn't loaded yet).
require_once FLEXILAY_WOO_PLUGIN_DIR . 'includes/core/class-flexilay-options.php';
require_once FLEXILAY_WOO_PLUGIN_DIR . 'includes/core/class-flexilay-node-client.php';
require_once FLEXILAY_WOO_PLUGIN_DIR . 'includes/rest/class-flexilay-rest-proxy.php';
require_once FLEXILAY_WOO_PLUGIN_DIR . 'includes/admin/class-flexilay-admin.php';

add_action( 'plugins_loaded', function () {
	// These are safe without WooCommerce classes.
	Flexilay_Woo_Options::init();
	Flexilay_Woo_Node_Client::init();
	Flexilay_Woo_Rest_Proxy::init();
	Flexilay_Woo_Admin::init();
}, 5 );

function flexilay_woo_is_woocommerce_active() : bool {
	if ( ! class_exists( 'WooCommerce' ) && ! class_exists( 'WC_Payment_Gateway' ) ) {
		return false;
	}
	// Verify minimum WooCommerce version
	if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '8.0', '<' ) ) {
		return false;
	}
	return true;
}

function flexilay_woo_admin_missing_woocommerce_notice() : void {
	if ( flexilay_woo_is_woocommerce_active() || ! current_user_can( 'activate_plugins' ) ) {
		return;
	}

	echo '<div class="notice notice-error"><p>';
	echo esc_html__( 'FlexiLay Gateway for WooCommerce requires WooCommerce to be installed and active before FlexiLay can be used.', 'flexilay-gateway-for-woocommerce' );
	echo '</p></div>';
}
add_action( 'admin_notices', 'flexilay_woo_admin_missing_woocommerce_notice' );

/**
 * Load WooCommerce gateway + optional Blocks integration only once WooCommerce is loaded.
 */
add_action( 'woocommerce_loaded', function () {
	if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
		return;
	}

	require_once FLEXILAY_WOO_PLUGIN_DIR . 'includes/core/class-flexilay-gateway.php';

	// Optional Blocks integration (file is guarded to avoid fatals).
	require_once FLEXILAY_WOO_PLUGIN_DIR . 'includes/class-flexilay-blocks.php';
	if ( class_exists( 'Flexilay_Blocks' ) ) {
		Flexilay_Blocks::init();
	}

	add_filter( 'woocommerce_payment_gateways', function ( $methods ) {
		$methods[] = 'Flexilay_WC_Gateway';
		return $methods;
	} );
}, 5 );
