<?php
/**
 * Plugin Name: Estimated Delivery Date for WooCommerce
 * Plugin URI: https://wordpress.org/plugins/wpactpro-estimated-delivery-date/
 * Description: Show shoppers a clear delivery-date estimate on product, cart, checkout and emails — with rule-based windows, a live preview and an urgency countdown.
 * Version: 1.0.0
 * Author: actpro
 * Author URI: https://profiles.wordpress.org/actpro/
 * Text Domain: wpactpro-estimated-delivery-date
 * Domain Path: /languages
 * Requires Plugins: woocommerce
 * Requires at least: 6.2
 * Requires PHP: 7.4
 * WC requires at least: 7.0
 * WC tested up to: 10.9.4
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 *
 * @fs_premium_only /addon/
 *
 * @package EstimatedDeliveryDateWC
 */

declare( strict_types=1 );

defined( 'ABSPATH' ) || exit;

! defined( 'EDFW_VERSION' ) && define( 'EDFW_VERSION', '1.0.0' );
! defined( 'EDFW_FILE' ) && define( 'EDFW_FILE', __FILE__ );
! defined( 'EDFW_DIR' ) && define( 'EDFW_DIR', plugin_dir_path( __FILE__ ) );
! defined( 'EDFW_URI' ) && define( 'EDFW_URI', plugin_dir_url( __FILE__ ) );
! defined( 'EDFW_SLUG' ) && define( 'EDFW_SLUG', 'wpactpro-estimated-delivery-date' );

/*
 * Freemius must initialise before anything else so it can register its own hooks and,
 * critically, so the add-on can find the SDK already loaded rather than loading a
 * second copy. Skipped under PHPUnit: the SDK registers admin UI and can reach the
 * network, neither of which belongs in a test run.
 */
if ( ! defined( 'WP_TESTS_DOMAIN' ) && file_exists( EDFW_DIR . 'freemius/start.php' ) ) {
	require_once EDFW_DIR . 'includes/edfw-freemius.php';
}

/*
 * Uninstall cleanup. Loaded unconditionally and at file scope, not inside edfw_init():
 * register_uninstall_hook() must run on every request for WordPress to remember the
 * callback, and Freemius's after_uninstall has to be attached before the SDK acts.
 */
require_once EDFW_DIR . 'includes/edfw-uninstall.php';

add_action( 'before_woocommerce_init', 'edfw_declare_compatibility' );

/**
 * Declare HPOS and Cart/Checkout Blocks compatibility.
 *
 * @since 1.0.0
 */
function edfw_declare_compatibility(): void {
	if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', EDFW_FILE, true );
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', EDFW_FILE, true );
	}
}

register_activation_hook( EDFW_FILE, 'edfw_activate' );

/**
 * On activation, seed a default "all products" delivery rule so a fresh install
 * shows an estimate out of the box.
 *
 * @since 1.0.0
 */
function edfw_activate(): void {
	if ( false === get_option( 'edfw_rules', false ) ) {
		update_option( 'edfw_rules', array( 'default' => edfw_default_rule( '2', '5' ) ) );
	}
}

/**
 * Shape of the catch-all default rule: one "Product is Any" condition, which
 * matches every product.
 *
 * @since 1.0.0
 * @param string $min Minimum business days.
 * @param string $max Maximum business days.
 * @return array<string, mixed>
 */
function edfw_default_rule( string $min, string $max ): array {
	return array(
		'conditions' => array(
			array(
				'type' => 'product',
				'op'   => 'is',
				'vals' => array(),
			),
		),
		'min'        => $min,
		'max'        => $max,
	);
}

add_action( 'plugins_loaded', 'edfw_init', 11 );

/**
 * Initialize plugin. Requires WooCommerce.
 *
 * @since 1.0.0
 */
function edfw_init(): void {
	if ( ! function_exists( 'WC' ) || ! version_compare( WC()->version, '7.0', '>=' ) ) {
		add_action( 'admin_notices', 'edfw_wc_required_notice' );
		return;
	}

	require_once EDFW_DIR . 'includes/class-edfw-helper.php';
	require_once EDFW_DIR . 'includes/class-edfw-backend.php';
	require_once EDFW_DIR . 'includes/class-edfw-frontend.php';
	require_once EDFW_DIR . 'includes/class-edfw-rest.php';
	require_once EDFW_DIR . 'includes/class-edfw-wc-api.php';

	EDFW_Helper::instance();
	EDFW_Backend::instance();
	EDFW_Frontend::instance();
	EDFW_REST::instance();
	EDFW_WC_API::instance();

	/*
	 * Premium features, when this build has them. Removed entirely from the
	 * wordpress.org version by the @fs_premium_only annotation above, so this
	 * file_exists() is simply false there — the free plugin never references
	 * anything inside /addon/.
	 */
	if ( file_exists( EDFW_DIR . 'addon/addon.php' ) ) {
		require_once EDFW_DIR . 'addon/addon.php';
	}

	/**
	 * Fires when Estimated Delivery for WooCommerce is fully loaded.
	 *
	 * Premium features and third-party add-ons hook here to register themselves via
	 * filters and actions.
	 *
	 * @since 1.0.0
	 */
	do_action( 'edfw_loaded' );
}

/**
 * Admin notice when WooCommerce is missing or outdated.
 *
 * @since 1.0.0
 */
function edfw_wc_required_notice(): void {
	?>
	<div class="notice notice-error">
		<p><strong><?php esc_html_e( 'Estimated Delivery Date', 'wpactpro-estimated-delivery-date' ); ?></strong>
			<?php esc_html_e( 'requires WooCommerce 7.0 or greater.', 'wpactpro-estimated-delivery-date' ); ?></p>
	</div>
	<?php
}
