<?php

/**
 * Fired when the plugin is uninstalled.
 *
 * Removes all plugin data: custom tables, options, cron events, and transients.
 */

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

global $wpdb;

// 1. Drop all custom tables
$tables = [
    'actionwear_log',
    'actionwear_categories_associations',
    'actionwear_categories_product',
    'actionwear_products',
    'actionwear_brands',
    'actionwear_categories',
    'actionwear_recharges_details',
    'actionwear_recharges',
    'actionwear_images',
    'actionwear_orders',
    'actionwear_addresses',
    'actionwear_jobs',
    'actionwear_sync_state',
];

$wpdb->query("SET FOREIGN_KEY_CHECKS = 0;");
foreach ($tables as $table) {
    $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}{$table}");
}
$wpdb->query("SET FOREIGN_KEY_CHECKS = 1;");

// 2. Delete all plugin options
$options = [
    // API & Authentication
    '_ACTIONWEAR_APIKEY',
    '_ACTIONWEAR_TOKEN_CODE',
    '_ACTIONWEAR_HUB_JWT',
    '_ACTIONWEAR_BASE_URL',

    // Product Configuration
    '_ACTIONWEAR_PRODUCTS_LANG',
    '_ACTIONWEAR_PRODUCTS_LANGUAGE',
    '_ACTIONWEAR_USE_CONFIGURABLE',
    '_ACTIONWEAR_IMAGES_CUSTOMIZATION',
    '_ACTIONWEAR_SYNC_FIELDS',

    // Price & Recharges
    '_ACTIONWEAR_RECHARGE_TYPE_SELECTED',
    '_ACTIONWEAR_LIST_TYPE_SELECTED',
    '_ACTIONWEAR_ARE_RECHARGE_TABLES_DISABLED',
    '_ACTIONWEAR_IS_PRICE_SYNC_DISABLED',

    // Supplier & Features
    '_ACTIONWEAR_SUPPLIER_AVAILABILITY',
    '_ACTIONWEAR_SUPPLIER_TYPE',
    '_ACTIONWEAR_ORDER_SYNC_TRIGGER',
    '_ACTIONWEAR_ORDER_TEST_MODE',

    // UI
    '_ACTIONWEAR_ATTRIBUTES_BOX_POSITION',

    // Logging & Debug
    '_ACTIONWEAR_LOG_WRITING',
    '_ACTIONWEAR_DEBUG_MODE',

    // Sync Progress & State
    '_ACTIONWEAR_INITIAL_SYNC_PROGRESS',
    '_ACTIONWEAR_ONBOARDING',
    '_ACTIONWEAR_LAST_SYNC',
    '_ACTIONWEAR_LAST_SEEN_VERSION',

    // Batch Timestamps
    '_ACTIONWEAR_LAST_BULK_PRICE_SYNC',
    '_ACTIONWEAR_LAST_BULK_PRICE_SYNC_COMPLETED',
    '_ACTIONWEAR_LAST_BULK_QUANTITY_SYNC',
    '_ACTIONWEAR_LAST_BULK_QUANTITY_SYNC_COMPLETED',

    // Differential Sync
    '_ACTIONWEAR_LAST_PRICE_UPDATE',
    '_ACTIONWEAR_LAST_QUANTITY_UPDATE',
    '_ACTIONWEAR_LAST_PRODUCT_DIFFERENTIAL_UPDATE',

    // Other State
    '_ACTIONWEAR_LAST_RESYNCED_PRODUCTS',
    '_ACTIONWEAR_LAST_RESET_SETTINGS',
    '_ACTIONWEAR_LAST_RECHARGES_RESET',
    '_ACTIONWEAR_LAST_LOG_PRUNE',
    '_ACTIONWEAR_LAST_ADMIN_DISPATCH',
    '_ACTIONWEAR_LAST_BG_CRASH',

    // Locks & Concurrency
    '_ACTIONWEAR_LOCK',
    '_actionwear_runner_lock',
    '_ACTIONWEAR_RUNNER_HEARTBEAT',
    '_ACTIONWEAR_CRONJOB_FROZEN',

    // Database & Versioning
    '_ACTIONWEAR_DB_VERSION',
    '_ACTIONWEAR_DB_UPGRADE_TEST_SUCCESS',

    // Category & Attributes
    '_ACTIONWEAR_DEFAULT_CATEGORY_ID',
    '_ACTIONWEAR_SELECTED_TAXONOMIES',

    // Error Handling & Circuit Breaker
    '_ACTIONWEAR_API_ERROR',
    '_ACTIONWEAR_CIRCUIT_FAIL',
    '_ACTIONWEAR_CIRCUIT_RETRY',

    // Background Processing
    '_ACTIONWEAR_BG_HEARTBEATS',
    '_ACTIONWEAR_BG_TIME_LIMIT',
];

foreach ($options as $option) {
    delete_option($option);
}

// 3. Clean up any remaining actionwear_ prefixed options (batch leftovers, transients, etc.)
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE 'actionwear\_%'");
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_ACTIONWEAR\_%'");
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_actionwear\_%'");
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_actionwear\_%'");

// 4. Unschedule all cron events
$cron_hooks = [
    'actionwear_runner_hook',
    'actionwear_scheduler_check',
];

foreach ($cron_hooks as $hook) {
    wp_clear_scheduled_hook($hook);
}
