<?php
/**
 * Uninstall script for Variation Hub
 * 
 * Fired when the plugin is uninstalled
 * 
 * @package VariationHub
 * @since 1.0.0
 */

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

// If uninstall not called from WordPress, exit
if (!defined('WP_UNINSTALL_PLUGIN')) {
    exit;
}

// Load cache table class
require_once plugin_dir_path(__FILE__) . 'includes/core/class-cache-table.php';

// Drop cache table
\VariationHub\Core\Cache_Table::drop_table();

// Delete plugin options (varihu prefix)
delete_option('varihu_version');
delete_option('varihu_installed_date');
delete_option('varihu_activated');
delete_option('varihu_cache_db_version');
delete_option('varihu_cache_last_sync');
delete_option('varihu_cache_sync_status');
delete_option('varihu_license_key');
delete_option('varihu_license_data');

// Clear scheduled hooks (varihu prefix)
wp_clear_scheduled_hook('varihu_cache_sync_batch');
wp_clear_scheduled_hook('varihu_sync_imported_variation');
wp_clear_scheduled_hook('varihu_validate_license_daily');

// Delete transients
global $wpdb;
$wpdb->query(
    "DELETE FROM {$wpdb->options} 
     WHERE option_name LIKE '_transient_varihu_%' 
     OR option_name LIKE '_transient_timeout_varihu_%'"
);

// Delete user meta (if any)
$wpdb->query(
    "DELETE FROM {$wpdb->usermeta} 
     WHERE meta_key LIKE 'varihu_%'"
);

// Clear Action Scheduler jobs (if exists)
if (function_exists('as_unschedule_all_actions')) {
    as_unschedule_all_actions('varihu_cache_sync_batch');
    as_unschedule_all_actions('varihu_sync_imported_variation');
}
