<?php

if (!defined('ABSPATH')) {
    exit;
}

if (!function_exists('iapc_is_woocommerce_available')) {
    function iapc_is_woocommerce_available() {
        if (!function_exists('is_plugin_active')) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }

        return is_plugin_active('woocommerce/woocommerce.php') && class_exists('WooCommerce');
    }
}

if (!function_exists('iapc_woocommerce_required_notice')) {
    function iapc_woocommerce_required_notice() {
        echo '<div class="notice notice-error"><p><strong>' .
            esc_html__('Idesoftbcn AI Product Customizer', 'idesoftbcn-ai-product-customizer') .
            '</strong> ' .
            esc_html__('requires WooCommerce to be installed and active.', 'idesoftbcn-ai-product-customizer') .
            '</p></div>';
    }
}

if (!function_exists('iapc_activate_plugin')) {
    function iapc_activate_plugin() {
        if (!iapc_is_woocommerce_available()) {
            if (!function_exists('deactivate_plugins')) {
                require_once ABSPATH . 'wp-admin/includes/plugin.php';
            }

            deactivate_plugins(plugin_basename(IAPC_PLUGIN_FILE));

            wp_die(
                esc_html__(
                    'Idesoftbcn AI Product Customizer requires WooCommerce to be installed and active.',
                    'idesoftbcn-ai-product-customizer'
                ),
                esc_html__('Plugin activation failed', 'idesoftbcn-ai-product-customizer'),
                array('back_link' => true)
            );
        }

        flush_rewrite_rules();
    }
}

if (!function_exists('iapc_check_woocommerce')) {
    function iapc_check_woocommerce() {
        if (iapc_is_woocommerce_available()) {
            return;
        }

        if (is_admin()) {
            add_action('admin_notices', 'iapc_woocommerce_required_notice');
        }
    }
}
