<?php
/**
 * Plugin Name: WooCommerce Licensed Product
 * Plugin URI: https://src.bundespruefstelle.ch/magdev/wc-licensed-product
 * Description: WooCommerce plugin to sell software products using license keys with domain-based validation.
 * Version: 0.5.4
 * Author: Marco Graetsch
 * Author URI: https://src.bundespruefstelle.ch/magdev
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: wc-licensed-product
 * Domain Path: /languages
 * Requires at least: 6.0
 * Requires PHP: 8.3
 * WC requires at least: 10.0
 * WC tested up to: 10.0
 *
 * @package Jeremias\WcLicensedProduct
 */

declare(strict_types=1);

namespace Jeremias\WcLicensedProduct;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Plugin constants
define('WC_LICENSED_PRODUCT_VERSION', '0.5.4');
define('WC_LICENSED_PRODUCT_PLUGIN_FILE', __FILE__);
define('WC_LICENSED_PRODUCT_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WC_LICENSED_PRODUCT_PLUGIN_URL', plugin_dir_url(__FILE__));
define('WC_LICENSED_PRODUCT_PLUGIN_BASENAME', plugin_basename(__FILE__));

// Load Composer autoloader
if (file_exists(WC_LICENSED_PRODUCT_PLUGIN_DIR . 'vendor/autoload.php')) {
    require_once WC_LICENSED_PRODUCT_PLUGIN_DIR . 'vendor/autoload.php';
}

/**
 * Check if WooCommerce is active
 */
function wc_licensed_product_is_woocommerce_active(): bool
{
    return class_exists('WooCommerce');
}

/**
 * Admin notice for missing WooCommerce
 */
function wc_licensed_product_woocommerce_missing_notice(): void
{
    ?>
    <div class="error">
        <p>
            <?php
            printf(
                /* translators: %s: WooCommerce plugin name */
                esc_html__('%s requires WooCommerce to be installed and active.', 'wc-licensed-product'),
                '<strong>WC Licensed Product</strong>'
            );
            ?>
        </p>
    </div>
    <?php
}

/**
 * Initialize the plugin
 */
function wc_licensed_product_init(): void
{
    // Check for WooCommerce
    if (!wc_licensed_product_is_woocommerce_active()) {
        add_action('admin_notices', __NAMESPACE__ . '\\wc_licensed_product_woocommerce_missing_notice');
        return;
    }

    // Load text domain
    load_plugin_textdomain(
        'wc-licensed-product',
        false,
        dirname(WC_LICENSED_PRODUCT_PLUGIN_BASENAME) . '/languages'
    );

    // Initialize the plugin
    Plugin::instance();
}

// Hook into plugins_loaded to ensure WooCommerce is loaded first
add_action('plugins_loaded', __NAMESPACE__ . '\\wc_licensed_product_init');

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

        // Declare cart/checkout blocks compatibility
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
            'cart_checkout_blocks',
            __FILE__,
            true
        );
    }
});

// Register activation hook
register_activation_hook(__FILE__, function (): void {
    if (!wc_licensed_product_is_woocommerce_active()) {
        deactivate_plugins(WC_LICENSED_PRODUCT_PLUGIN_BASENAME);
        wp_die(
            esc_html__('WC Licensed Product requires WooCommerce to be installed and active.', 'wc-licensed-product'),
            'Plugin Activation Error',
            ['back_link' => true]
        );
    }

    // Run installation
    Installer::activate();
});

// Register deactivation hook
register_deactivation_hook(__FILE__, function (): void {
    Installer::deactivate();
});

// Add Settings link to plugin actions
add_filter('plugin_action_links_' . plugin_basename(__FILE__), function (array $links): array {
    $settingsUrl = admin_url('admin.php?page=wc-settings&tab=licensed_product');
    $settingsLink = '<a href="' . esc_url($settingsUrl) . '">' . esc_html__('Settings', 'wc-licensed-product') . '</a>';
    array_unshift($links, $settingsLink);
    return $links;
});
