<?php
/**
 * Kramar
 *
 * @package     Kramar
 * @license     GPL-2.0-or-later
 *
 * @wordpress-plugin
 * Plugin Name: Kramar - Toolkit for eCommerce: Invoices, Email, Compliance & More
 * Plugin URI:  https://wordpress.org/plugins/kramar/
 * Description: EU/NL compliance, PDF invoices, a drag-and-drop email builder, checkout field editor, custom order statuses, product & performance tools — 27 modules in one lean plugin for WooCommerce.
 * Version:     1.0.3
 * Requires at least: 7.0
 * Requires PHP: 8.4
 * Requires Plugins: woocommerce
 * WC requires at least: 9.0
 * WC tested up to: 10.8
 * Author: Naiche
 * Author URI: https://profiles.wordpress.org/naiches/
 * Text Domain: kramar
 * Domain Path: /languages
 * License:     GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */

declare( strict_types=1 );

// Prevent direct access.
defined( 'ABSPATH' ) || exit;

// Plugin constants.
define( 'KRAMAR_VERSION', '1.0.3' );
define( 'KRAMAR_FILE', __FILE__ );
define( 'KRAMAR_DIR', plugin_dir_path( __FILE__ ) );
define( 'KRAMAR_URL', plugin_dir_url( __FILE__ ) );
define( 'KRAMAR_BASENAME', plugin_basename( __FILE__ ) );

// Minimum PHP version check.
if ( version_compare( PHP_VERSION, '8.4', '<' ) ) {
	add_action(
		'admin_notices',
		static function (): void {
			printf(
				'<div class="notice notice-error"><p>%s</p></div>',
				esc_html(
					sprintf(
						/* translators: %s: Required PHP version. */
						__( 'Kramar requires PHP %s or higher. Please update your PHP version.', 'kramar' ),
						'8.4'
					)
				)
			);
		}
	);
	return;
}

// Declare HPOS compatibility (must run before WooCommerce init, so register early).
add_action(
	'before_woocommerce_init',
	static function (): void {
		if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', KRAMAR_FILE, true );
		}
	}
);

// Register activation hook (stores default settings on first activation).
register_activation_hook(
	KRAMAR_FILE,
	static function (): void {
		require_once KRAMAR_DIR . 'includes/class-module.php';
		require_once KRAMAR_DIR . 'includes/class-settings.php';
		require_once KRAMAR_DIR . 'modules/performance/class-cart-fragments.php';
		require_once KRAMAR_DIR . 'modules/performance/class-dequeue-assets.php';
		require_once KRAMAR_DIR . 'modules/performance/class-wc-cleanup.php';
		require_once KRAMAR_DIR . 'modules/compliance/class-privacy-hardening.php';
		require_once KRAMAR_DIR . 'modules/compliance/class-order-button.php';
		require_once KRAMAR_DIR . 'modules/compliance/class-legal-checkboxes.php';
		require_once KRAMAR_DIR . 'modules/compliance/class-eu-product-compliance.php';
		require_once KRAMAR_DIR . 'modules/checkout/class-address-validation.php';
		require_once KRAMAR_DIR . 'modules/checkout/class-field-editor.php';
		require_once KRAMAR_DIR . 'modules/checkout/class-vat-id.php';
		require_once KRAMAR_DIR . 'modules/checkout/class-order-delivery-date.php';
		require_once KRAMAR_DIR . 'modules/orders/class-sequential-numbers.php';
		require_once KRAMAR_DIR . 'modules/orders/class-custom-statuses.php';
		require_once KRAMAR_DIR . 'modules/orders/class-refund-return.php';
		require_once KRAMAR_DIR . 'modules/products/class-min-max-quantities.php';
		require_once KRAMAR_DIR . 'modules/products/class-extra-options.php';
		require_once KRAMAR_DIR . 'modules/products/class-swatches.php';
		require_once KRAMAR_DIR . 'modules/products/class-omnibus.php';
		require_once KRAMAR_DIR . 'modules/products/class-enhanced-reviews.php';
		require_once KRAMAR_DIR . 'modules/email/class-email-manager.php';
		require_once KRAMAR_DIR . 'modules/email/class-custom-email-triggers.php';
		require_once KRAMAR_DIR . 'modules/marketing/class-google-shopping-feed.php';
		require_once KRAMAR_DIR . 'modules/marketing/google-shopping-feed/class-feed-config.php';
		require_once KRAMAR_DIR . 'modules/marketing/google-shopping-feed/class-feed-delivery.php';
		require_once KRAMAR_DIR . 'modules/marketing/google-shopping-feed/class-product-query.php';
		require_once KRAMAR_DIR . 'modules/marketing/google-shopping-feed/class-product-data.php';
		require_once KRAMAR_DIR . 'modules/marketing/google-shopping-feed/class-variation-handler.php';
		require_once KRAMAR_DIR . 'modules/marketing/google-shopping-feed/class-xml-writer.php';
		require_once KRAMAR_DIR . 'modules/marketing/google-shopping-feed/class-feed-generator.php';
		require_once KRAMAR_DIR . 'modules/marketing/class-back-in-stock.php';
		require_once KRAMAR_DIR . 'modules/marketing/class-abandoned-cart.php';
		require_once KRAMAR_DIR . 'modules/marketing/class-review-reminder.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-number-store.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-storage.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-document.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-invoice.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-packing-slip.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-credit-note.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-block-renderer.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-pdf-renderer.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-pdf-invoices.php';
		require_once KRAMAR_DIR . 'modules/account/class-account-flow.php';
		require_once KRAMAR_DIR . 'modules/shipping/class-weight-based-shipping.php';
		require_once KRAMAR_DIR . 'includes/class-plugin.php';
		Kramar\Modules\Products\Omnibus::install();
		Kramar\Modules\Marketing\Back_In_Stock::install();
		Kramar\Modules\Marketing\Abandoned_Cart::install();
		Kramar\Modules\Marketing\Review_Reminder::install();
		Kramar\Plugin::activate();
	}
);

// Register deactivation hook.
register_deactivation_hook(
	KRAMAR_FILE,
	static function (): void {
		// Unschedule everything the modules register. Without this, a deactivated
		// plugin leaves WP-Cron rows in wp_options and Action Scheduler entries
		// behind indefinitely, firing hooks nothing listens to any more — and
		// they accumulate again on every reactivate.
		foreach ( [
			'kramar_omnibus_check',
			'kramar_cleanup_sessions',
			'kramar_cleanup_analytics',
			'kramar_cleanup_geolocation',
			'kramar_acr_abandon_check',
			'kramar_acr_send_email',
			'kramar_acr_retention',
			'kramar_bis_notify_batch',
			'kramar_bis_discontinued_batch',
			'kramar_bis_retention',
			'kramar_send_review_reminder',
			'kramar_send_custom_email',
			'kramar_google_feed_generate',
		] as $hook ) {
			wp_clear_scheduled_hook( $hook );
			if ( function_exists( 'as_unschedule_all_actions' ) ) {
				as_unschedule_all_actions( $hook );
			}
		}

		if ( function_exists( 'as_unschedule_all_actions' ) ) {
			as_unschedule_all_actions( '', [], 'kramar' );
		}
	}
);

// Boot on plugins_loaded so WooCommerce is guaranteed to be available
// regardless of alphabetical plugin load order.
add_action(
	'plugins_loaded',
	static function (): void {
		if ( ! class_exists( 'WooCommerce' ) ) {
			add_action(
				'admin_notices',
				static function (): void {
					printf(
						'<div class="notice notice-error"><p>%s</p></div>',
						esc_html__( 'Kramar requires WooCommerce to be installed and active.', 'kramar' )
					);
				}
			);
			return;
		}

		require_once KRAMAR_DIR . 'includes/class-module.php';
		require_once KRAMAR_DIR . 'includes/class-settings.php';
		require_once KRAMAR_DIR . 'modules/performance/class-cart-fragments.php';
		require_once KRAMAR_DIR . 'modules/performance/class-dequeue-assets.php';
		require_once KRAMAR_DIR . 'modules/performance/class-wc-cleanup.php';
		require_once KRAMAR_DIR . 'modules/compliance/class-privacy-hardening.php';
		require_once KRAMAR_DIR . 'modules/compliance/class-order-button.php';
		require_once KRAMAR_DIR . 'modules/compliance/class-legal-checkboxes.php';
		require_once KRAMAR_DIR . 'modules/compliance/class-eu-product-compliance.php';
		require_once KRAMAR_DIR . 'modules/checkout/class-address-validation.php';
		require_once KRAMAR_DIR . 'modules/checkout/class-field-editor.php';
		require_once KRAMAR_DIR . 'modules/checkout/class-vat-id.php';
		require_once KRAMAR_DIR . 'modules/checkout/class-order-delivery-date.php';
		require_once KRAMAR_DIR . 'modules/orders/class-sequential-numbers.php';
		require_once KRAMAR_DIR . 'modules/orders/class-custom-statuses.php';
		require_once KRAMAR_DIR . 'modules/orders/class-refund-return.php';
		require_once KRAMAR_DIR . 'modules/products/class-min-max-quantities.php';
		require_once KRAMAR_DIR . 'modules/products/class-extra-options.php';
		require_once KRAMAR_DIR . 'modules/products/class-swatches.php';
		require_once KRAMAR_DIR . 'modules/products/class-omnibus.php';
		require_once KRAMAR_DIR . 'modules/products/class-enhanced-reviews.php';
		require_once KRAMAR_DIR . 'modules/email/class-email-manager.php';
		require_once KRAMAR_DIR . 'modules/email/class-custom-email-triggers.php';
		require_once KRAMAR_DIR . 'modules/marketing/class-google-shopping-feed.php';
		require_once KRAMAR_DIR . 'modules/marketing/google-shopping-feed/class-feed-config.php';
		require_once KRAMAR_DIR . 'modules/marketing/google-shopping-feed/class-feed-delivery.php';
		require_once KRAMAR_DIR . 'modules/marketing/google-shopping-feed/class-product-query.php';
		require_once KRAMAR_DIR . 'modules/marketing/google-shopping-feed/class-product-data.php';
		require_once KRAMAR_DIR . 'modules/marketing/google-shopping-feed/class-variation-handler.php';
		require_once KRAMAR_DIR . 'modules/marketing/google-shopping-feed/class-xml-writer.php';
		require_once KRAMAR_DIR . 'modules/marketing/google-shopping-feed/class-feed-generator.php';
		require_once KRAMAR_DIR . 'modules/marketing/class-back-in-stock.php';
		require_once KRAMAR_DIR . 'modules/marketing/class-abandoned-cart.php';
		require_once KRAMAR_DIR . 'modules/marketing/class-review-reminder.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-number-store.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-storage.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-document.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-invoice.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-packing-slip.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-credit-note.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-block-renderer.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-pdf-renderer.php';
		require_once KRAMAR_DIR . 'modules/invoices/class-pdf-invoices.php';
		require_once KRAMAR_DIR . 'modules/account/class-account-flow.php';
		require_once KRAMAR_DIR . 'modules/shipping/class-weight-based-shipping.php';
		require_once KRAMAR_DIR . 'includes/class-omnibus-table.php';
		require_once KRAMAR_DIR . 'includes/class-plugin.php';
		require_once KRAMAR_DIR . 'includes/api.php';

		Kramar\Plugin::get_instance();
	}
);
