<?php
/**
 * Repetito
 *
 * @package           Repetito
 * @author            Repetito
 * @license           GPL-2.0-or-later
 *
 * @wordpress-plugin
 * Plugin Name:       Repetito Reusable Packaging
 * Description:       Add reusable packaging options to your WooCommerce checkout.
 * Version:           1.8.4
 * Requires at least: 6.5
 * Requires PHP:      7.4
 * Author:            Repetito
 * Author URI:        https://www.repetito.sk
 * Text Domain:       repetito
 * Domain Path:       /languages
 * Requires Plugins:  woocommerce
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 */

/*
|--------------------------------------------------------------------------
| If this file is called directly, abort.
|--------------------------------------------------------------------------
*/
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/*
|--------------------------------------------------------------------------
| Load class autoloader
|--------------------------------------------------------------------------
*/
require_once __DIR__ . '/vendor/autoload.php';
use Repetito\Plugin;

/*
|--------------------------------------------------------------------------
| Check for WooCommerce dependency
|--------------------------------------------------------------------------
*/
function repetito_check_woocommerce() {
	// Check if WooCommerce plugin is active using multiple methods
	if (!function_exists('is_plugin_active')) {
		require_once(ABSPATH . 'wp-admin/includes/plugin.php');
	}

	// Check both plugin active status and class existence
	$woocommerce_plugin_active = is_plugin_active('woocommerce/woocommerce.php');
	$woocommerce_class_exists = class_exists('WooCommerce');

	// WooCommerce is active if either check passes (plugin active OR class exists)
	return $woocommerce_plugin_active || $woocommerce_class_exists;
}

/*
|--------------------------------------------------------------------------
| Initialize plugin after plugins are loaded (to ensure WooCommerce is available)
|--------------------------------------------------------------------------
*/
function repetito_init_plugin() {
	if (!repetito_check_woocommerce()) {
		add_action('admin_notices', 'repetito_woocommerce_missing_notice');

		// Deactivate plugin if WooCommerce is not active during activation.
		if ( is_admin() && isset( $_GET['activate'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			unset( $_GET['activate'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			deactivate_plugins( plugin_basename( __FILE__ ) );
		}

		return;
	}

	// WooCommerce is available, initialize the plugin
	Plugin::init();
}

/*
|--------------------------------------------------------------------------
| Admin notice for missing WooCommerce
|--------------------------------------------------------------------------
*/
function repetito_woocommerce_missing_notice() {
	?>
	<div class="error">
		<p>
			<strong><?php esc_html_e('Repetito', 'repetito'); ?></strong>
			<?php esc_html_e('requires WooCommerce to be installed and active. Please install and activate WooCommerce to use this plugin.', 'repetito'); ?>
		</p>
	</div>
	<?php
}

/*
|--------------------------------------------------------------------------
| Load initialization scripts
|--------------------------------------------------------------------------
*/
require_once __DIR__ . '/includes/init/seed-default-product.php';
require_once __DIR__ . '/includes/init/seed-norepetito-tag.php';
/*
|--------------------------------------------------------------------------
| Define default constants
|--------------------------------------------------------------------------
*/
define('Repetito\NAME', 'repetito');
define('Repetito\VERSION', '1.8.4');
define('Repetito\FILE', __FILE__);
define('Repetito\PLUGIN_URL', plugin_dir_url(__FILE__));
define('Repetito\NOREPETITO_TAG', 'norepetito');
define('Repetito\API_URL', 'https://api.repetito.sk');
define('Repetito\PRIVACY_POLICY_URL', 'https://repetito.sk/gdpr');


/*
|--------------------------------------------------------------------------
| Activation, deactivation and uninstall event.
|--------------------------------------------------------------------------
*/
register_activation_hook(__FILE__, array(Plugin::class, 'activate'));
register_deactivation_hook(__FILE__, array(Plugin::class, 'deactivate'));
register_uninstall_hook(__FILE__, array(Plugin::class, 'uninstall'));

/*
|--------------------------------------------------------------------------
| Start the plugin after plugins are loaded (only if WooCommerce is active)
|--------------------------------------------------------------------------
*/
add_action('plugins_loaded', 'repetito_init_plugin', 20);
