<?php
/**
 * Plugin Name:       My Easy Monogram
 * Plugin URI:        https://app.myeasymonogram.com/integrations/woocommerce
 * Description:       Connects your WooCommerce store to My Easy Monogram for order management, production, shipping, and product personalization live preview.
 * Version:           1.0.2
 * Requires at least: 5.8
 * Requires PHP:      7.4
 * Author:            My Easy Monogram
 * Author URI:        https://app.myeasymonogram.com
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       my-easy-monogram
 * Domain Path:       /languages
 * Requires Plugins:  woocommerce
 *
 * WC requires at least: 7.0
 * WC tested up to:      9.6
 *
 * @package Memwc_Connector
 */

defined( 'ABSPATH' ) || exit;

// =========================================================================
// Constants
// =========================================================================

define( 'MEMWC_CONNECTOR_VERSION', '1.0.2' );
define( 'MEMWC_CONNECTOR_FILE', __FILE__ );
define( 'MEMWC_CONNECTOR_PATH', plugin_dir_path( __FILE__ ) );
define( 'MEMWC_CONNECTOR_URL', plugin_dir_url( __FILE__ ) );
define( 'MEMWC_CONNECTOR_BASENAME', plugin_basename( __FILE__ ) );

if ( ! defined( 'MEMWC_API_URL' ) ) {
	define( 'MEMWC_API_URL', 'https://app.myeasymonogram.com' );
}

/**
 * Base URL of the My Easy Monogram preview image service used by the Live
 * Preview widget. Defaults to production; a staging site can point the widget
 * at the staging service by defining this in wp-config.php before this file
 * loads, without needing a different plugin build.
 */
if ( ! defined( 'MEMWC_PREVIEW_API_URL' ) ) {
	define( 'MEMWC_PREVIEW_API_URL', 'https://42939t6nbh.execute-api.us-east-2.amazonaws.com' );
}

// =========================================================================
// Requirements check
// =========================================================================

function memwc_connector_check_requirements() {
	if ( ! class_exists( 'WooCommerce' ) ) {
		add_action( 'admin_notices', function () {
			printf(
				'<div class="notice notice-error"><p><strong>My Easy Monogram</strong> requires <strong>WooCommerce</strong> to be installed and active.</p></div>'
			);
		});
		return false;
	}

	if ( version_compare( WC_VERSION, '7.0', '<' ) ) {
		add_action( 'admin_notices', function () {
			printf(
				'<div class="notice notice-error"><p><strong>My Easy Monogram</strong> requires <strong>WooCommerce 7.0</strong> or higher.</p></div>'
			);
		});
		return false;
	}

	return true;
}

// =========================================================================
// Initialization
// =========================================================================

function memwc_connector_init() {
	if ( ! memwc_connector_check_requirements() ) {
		return;
	}

	require_once MEMWC_CONNECTOR_PATH . 'includes/class-mem-api.php';
	require_once MEMWC_CONNECTOR_PATH . 'includes/class-mem-webhooks.php';
	require_once MEMWC_CONNECTOR_PATH . 'includes/class-mem-admin.php';
	require_once MEMWC_CONNECTOR_PATH . 'includes/class-mem-connector.php';
	require_once MEMWC_CONNECTOR_PATH . 'includes/class-mem-live-preview.php';
	require_once MEMWC_CONNECTOR_PATH . 'includes/class-mem-order-tracking.php';
	require_once MEMWC_CONNECTOR_PATH . 'includes/class-mem-store-sync.php';

	Memwc_Connector::get_instance();

	// Store details sync — runs in admin to detect setting/version changes.
	if ( is_admin() ) {
		Memwc_Store_Sync::get_instance();
	}

	// Frontend-only classes — skip on admin pages (except AJAX) to avoid overhead.
	if ( ! is_admin() || wp_doing_ajax() ) {
		Memwc_Live_Preview_WooCommerce::get_instance();
		Memwc_Order_Tracking::get_instance();
	}
}
add_action( 'plugins_loaded', 'memwc_connector_init' );

// =========================================================================
// Activation / Deactivation
// =========================================================================

function memwc_connector_activate() {
	if ( ! class_exists( 'WooCommerce' ) ) {
		deactivate_plugins( plugin_basename( __FILE__ ) );
		wp_die(
			'My Easy Monogram requires WooCommerce to be installed and active.',
			'Plugin Activation Error',
			array( 'back_link' => true )
		);
	}

	add_option( 'memwc_connector_connected', false, '', true );
	add_option( 'memwc_shop_url', esc_url_raw( get_site_url() ), '', true );
	update_option( 'memwc_connector_version', MEMWC_CONNECTOR_VERSION, true );
}
register_activation_hook( __FILE__, 'memwc_connector_activate' );

function memwc_connector_deactivate() {
	// Does not disconnect from MEM. Deactivation is reversible — the merchant
	// can reactivate the plugin and resume immediately without reconnecting.
	// Full disconnect only happens on uninstall (plugin deletion).
}
register_deactivation_hook( __FILE__, 'memwc_connector_deactivate' );

// =========================================================================
// WooCommerce feature 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 );
	}
});
