<?php
/**
 * Uninstall handler. Runs when an operator deletes the plugin from the
 * WordPress admin (NOT on a simple deactivate). We remove every option,
 * transient, cron event, and custom table the connector creates —
 * including Enhanced Native / local-index storage.
 *
 * Credentials are NOT preserved across uninstall on purpose: a reinstall
 * runs through the Connect-to-Seekmodo flow again to regenerate the
 * tenant/secret pair, which is the safer default for support handoffs.
 *
 * Kept procedural (no autoload) so it works even if `src/` was already
 * removed mid-delete.
 *
 * @package Numinix\SeekmodoWordPress
 */

declare(strict_types=1);

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

global $wpdb;

$options = [
    // Pairing + core config (Options::OPT_*)
    'seekmodo_wp_tenant_id',
    'seekmodo_wp_shared_secret',
    'seekmodo_wp_install_token',
    'seekmodo_wp_install_token_exp',
    'seekmodo_wp_mode',
    'seekmodo_wp_auto_promote',
    'seekmodo_wp_gateway_url',
    'seekmodo_wp_indexable_post_types',
    'seekmodo_wp_post_type_priority',
    'seekmodo_wp_storefront_host',
    'seekmodo_wp_health_metrics',
    'seekmodo_wp_breaker_state',
    'seekmodo_wp_indexer_cursor',
    'seekmodo_wp_schema_version',
    'seekmodo_wp_commerce_schema_version',
    'seekmodo_wp_last_snapshot_at',
    'seekmodo_wp_enabled_verticals',
    'seekmodo_wp_show_branding',
    'seekmodo_wp_suggest_layout',
    'seekmodo_wp_suggest_show_branding',
    'seekmodo_wp_typeahead_ui_enabled',
    'seekmodo_wp_ask_ai_enabled',
    'seekmodo_wp_recommendations_enabled',
    'seekmodo_wp_suggest_legacy',
    'seekmodo_wp_fallback_bundle',
    // Non-Options::* rows written elsewhere
    'seekmodo_wp_remote_breaker',
    'seekmodo_wp_autopromote_state',
    'seekmodo_wp_active_version',
    // Local index / Enhanced Native
    'seekmodo_local_index_enabled',
    'seekmodo_local_index_mode',
    'seekmodo_local_index_content_enabled',
    'seekmodo_local_index_commerce_enabled',
    'seekmodo_relevanssi_compat_enabled',
    'seekmodo_local_index_schema_version',
    'seekmodo_local_index_cursor',
    'seekmodo_local_index_needs_full_build',
    'seekmodo_local_index_weights',
    'seekmodo_local_index_default_operator',
    'seekmodo_local_index_or_fallback',
    'seekmodo_local_index_partial_match',
    'seekmodo_local_index_throttle',
    'seekmodo_local_index_min_word_length',
    'seekmodo_local_index_comments',
    'seekmodo_local_index_custom_fields',
    'seekmodo_local_index_excluded_ids',
    'seekmodo_local_index_log_queries',
];

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

$transients = [
    'seekmodo_wp_tenant_snapshot',
    'seekmodo_wp_breaker_open_until',
    'seekmodo_wp_update_info_v1',
];

foreach ($transients as $key) {
    delete_transient($key);
}

$cron_hooks = [
    'seekmodo_wp_index_tick',
    'seekmodo_wp_tenant_snapshot_pull',
    'seekmodo_wp_events_flush',
    'seekmodo_wp_local_index_tick',
    'seekmodo_wp_commerce_index_tick',
];

foreach ($cron_hooks as $hook) {
    $timestamp = wp_next_scheduled($hook);
    while ($timestamp !== false) {
        wp_unschedule_event($timestamp, $hook);
        $timestamp = wp_next_scheduled($hook);
    }
    wp_clear_scheduled_hook($hook);
}

$prefix = $wpdb->prefix;
$tables = [
    $prefix . 'seekmodo_events',
    $prefix . 'seekmodo_index',
    $prefix . 'seekmodo_stopwords',
    $prefix . 'seekmodo_search_log',
    $prefix . 'seekmodo_synonyms',
    $prefix . 'seekmodo_pins',
    $prefix . 'seekmodo_rescue',
];

foreach ($tables as $table) {
    // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table names from our own prefix list
    $wpdb->query("DROP TABLE IF EXISTS {$table}");
}
