<?php
/**
 * Plugin Name: ShippingGuard - Advanced Shipping Suite for WooCommerce
 * Description: Ultimate WooCommerce logistics suite: Weight rules, Shipping Protection, Eco-Delivery, and Delivery Estimates.
 * Version: 2.2.2
 * Author: Dominopress
 * Plugin URI: https://dominopress.com/plugin/shippingguard
 * Author URI: https://dominopress.com
 * Tested up to: 6.9
 * WC requires at least: 8.0
 * WC tested up to: 10.6.0
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Text Domain: shippingguard
 * Requires Plugins: woocommerce
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

// Declare compatibility with WooCommerce High-Performance Order Storage (HPOS)
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);
    }
});

define('SHIPPINGGUARD_VERSION', '2.2.0');
define('SHIPPINGGUARD_FILE', __FILE__);
define('SHIPPINGGUARD_PATH', plugin_dir_path(SHIPPINGGUARD_FILE));
define('SHIPPINGGUARD_URL', plugin_dir_url(SHIPPINGGUARD_FILE));

function shippingguard_install()
{
    global $wpdb;
    $table_name = $wpdb->prefix . 'shippingguard_claims';
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
        order_id bigint(20) unsigned NOT NULL,
        customer_id bigint(20) unsigned NOT NULL,
        reason varchar(100) NOT NULL,
        status varchar(50) DEFAULT 'Pending' NOT NULL,
        attachment_id bigint(20) unsigned DEFAULT 0 NOT NULL,
        created_at datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";

    $table_orders = $wpdb->prefix . 'shippingguard_orders';
    $sql_orders = "CREATE TABLE $table_orders (
        order_id bigint(20) unsigned NOT NULL,
        is_protected tinyint(1) DEFAULT 0 NOT NULL,
        fee_amount decimal(10,2) DEFAULT 0.00 NOT NULL,
        protection_status varchar(50) DEFAULT 'unprotected' NOT NULL,
        PRIMARY KEY  (order_id)
    ) $charset_collate;";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
    dbDelta($sql_orders);

    // Set default module states
    if (false === get_option('shippingguard_module_protection')) {
        update_option('shippingguard_module_protection', 'yes');
    }
    if (false === get_option('shippingguard_module_weight_shipping')) {
        update_option('shippingguard_module_weight_shipping', 'yes');
    }
    if (false === get_option('shippingguard_module_zero_emission')) {
        update_option('shippingguard_module_zero_emission', 'yes');
    }
    if (false === get_option('shippingguard_module_edd')) {
        update_option('shippingguard_module_edd', 'yes');
    }

    // Register query var and endpoint once
    add_rewrite_endpoint('shippingguard-claims', EP_ROOT | EP_PAGES);

    // We update a flag to flush on the next init instead of doing it blindly here
    update_option('shippingguard_flush_rewrite_rules', 'yes');
}
register_activation_hook(SHIPPINGGUARD_FILE, 'shippingguard_install');

function shippingguard_init()
{
    // Check if WooCommerce is active
    if (!class_exists('WooCommerce')) {
        add_action('admin_notices', 'shippingguard_missing_wc_notice');
        return;
    }

    require_once SHIPPINGGUARD_PATH . 'includes/class-shippingguard-admin.php';
    require_once SHIPPINGGUARD_PATH . 'includes/class-shippingguard-frontend.php';
    require_once SHIPPINGGUARD_PATH . 'includes/class-shippingguard-api.php';
    require_once SHIPPINGGUARD_PATH . 'includes/class-shippingguard-claims-api.php';
    require_once SHIPPINGGUARD_PATH . 'includes/class-shippingguard-customer.php';
    require_once SHIPPINGGUARD_PATH . 'includes/class-shippingguard-emails.php';
    require_once SHIPPINGGUARD_PATH . 'includes/class-shippingguard-analytics-api.php';
    require_once SHIPPINGGUARD_PATH . 'includes/class-shippingguard-feedback-api.php';
    require_once SHIPPINGGUARD_PATH . 'includes/class-shippingguard-blocks.php';

    $admin = new ShippingGuard_Admin();
    $admin->init();

    $frontend = new ShippingGuard_Frontend();
    $frontend->init();

    $api = new ShippingGuard_API();
    $api->init();

    $claims_api = new ShippingGuard_Claims_API();
    $claims_api->init();

    $customer = new ShippingGuard_Customer();
    $customer->init();

    $emails = new ShippingGuard_Emails();
    $emails->init();

    $analytics_api = new ShippingGuard_Analytics_API();
    $analytics_api->init();

    $blocks = new ShippingGuard_Blocks();
    $blocks->init();
}
add_action('plugins_loaded', 'shippingguard_init');

/**
 * Register Rewrite Endpoints
 */
function shippingguard_register_endpoints() {
    add_rewrite_endpoint('shippingguard-claims', EP_ROOT | EP_PAGES);

    if ('yes' === get_option('shippingguard_flush_rewrite_rules')) {
        flush_rewrite_rules();
        delete_option('shippingguard_flush_rewrite_rules');
    }
}
add_action('init', 'shippingguard_register_endpoints');

/**
 * Check if Pro addon is active
 */
function shippingguard_is_pro()
{
    return apply_filters('shippingguard_is_pro', false);
}

/**
 * Add Settings link to plugin action links
 */
function shippingguard_add_action_links($links)
{
    $settings_link = array(
        '<a href="' . admin_url('admin.php?page=shippingguard-settings') . '">' . __('Settings', 'shippingguard') . '</a>',
    );
    return array_merge($settings_link, $links);
}
add_filter('plugin_action_links_' . plugin_basename(SHIPPINGGUARD_FILE), 'shippingguard_add_action_links');

/**
 * Show admin notice if WooCommerce is missing
 */
function shippingguard_missing_wc_notice()
{
    ?>
    <div class="error">
        <p><?php echo esc_html__('ShippingGuard requires WooCommerce to be installed and active.', 'shippingguard'); ?></p>
    </div>
    <?php
}
