<?php
/**
 * Plugin Name: Woocom-Pay
 * Plugin URI:  https://wpplugin.touch2me.com/woocom-pay/
 * Description: Accept Full and Partial Payments through any UPI app without requiring a payment gateway account.
 * Version:     1.0.0
 * Author:      Touch2me Technologies
 * Author URI:  https://touch2me.com
 * Text Domain: woocom-pay
 * Domain Path: /languages
 * Requires at least: 6.8
 * Requires PHP: 8.2
 * WC requires at least: 10.0
 * WC tested up to: 10.0
 */

defined( 'ABSPATH' ) || exit;

define( 'WOOCOM_PAY_VERSION', '2.0.0' );
define( 'WOOCOM_PAY_PLUGIN_FILE', __FILE__ );
define( 'WOOCOM_PAY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'WOOCOM_PAY_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

// Declare HPOS compatibility.
add_action( 'before_woocommerce_init', function () {
	if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
	}
} );

// Load activator/deactivator early — before their hooks are registered.
require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/class-woocom-pay-activator.php';
require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/class-woocom-pay-deactivator.php';

// Activation / deactivation hooks.
register_activation_hook( __FILE__, [ 'Woocom_Pay_Activator', 'activate' ] );
register_deactivation_hook( __FILE__, [ 'Woocom_Pay_Deactivator', 'deactivate' ] );

/**
 * Bootstrap the plugin after WooCommerce is loaded.
 */
function woocom_pay_init() {
	if ( ! class_exists( 'WooCommerce' ) ) {
		add_action( 'admin_notices', function () {
			echo '<div class="notice notice-error"><p>' .
				esc_html__( 'Woocom-Pay requires WooCommerce to be installed and active.', 'woocom-pay' ) .
				'</p></div>';
		} );
		return;
	}

	require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/class-woocom-pay-db.php';
	require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/class-woocom-pay-order-status.php';
	require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/class-woocom-pay-emails.php';
	require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/class-woocom-pay-product.php';
	require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/class-woocom-pay-gateway.php';
	require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/class-woocom-pay-balance.php';
	require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/class-woocom-pay-admin.php';

	// License manager (must load before gateway manager).
	require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/class-wcp-license.php';

	// Payment gateway drivers.
	require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/gateways/interface-wcp-payment-gateway.php';
	require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/gateways/class-wcp-gateway-upi.php';
	require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/gateways/class-wcp-gateway-razorpay.php';
	require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/gateways/class-wcp-gateway-cashfree.php';
	require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/gateways/class-wcp-gateway-custom.php';
	require_once WOOCOM_PAY_PLUGIN_DIR . 'includes/class-wcp-gateway-manager.php';

	// Register WooCommerce payment gateway.
	add_filter( 'woocommerce_payment_gateways', function ( $gateways ) {
		$gateways[] = 'Woocom_Pay_Gateway';
		return $gateways;
	} );

	// Boot subsystems.
	Woocom_Pay_Order_Status::init();
	Woocom_Pay_Product::init();
	Woocom_Pay_Balance::init();
	Woocom_Pay_Emails::init();
	WCP_Gateway_Manager::init();

	if ( is_admin() ) {
		Woocom_Pay_Admin::init();
	}
}
add_action( 'plugins_loaded', 'woocom_pay_init' );

/**
 * Helper: plugin settings option.
 */
function woocom_pay_get_option( string $key, $default = '' ) {
	$settings = get_option( 'woocom_pay_settings', [] );
	return $settings[ $key ] ?? $default;
}
