<?php
/**
 * Uninstall handler.
 *
 * Fired when the plugin is deleted via the WordPress admin.
 * Removes all plugin options, transients, and data.
 *
 * @package KwiroAISalesAgent
 */

// Abort if not called by WordPress.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}

// Remove all plugin options.
delete_option( 'kwiro_api_key' );
delete_option( 'kwiro_store_id' );
delete_option( 'kwiro_settings' );
delete_option( 'kwiro_sync_status' );
delete_option( 'kwiro_version' );
delete_option( 'kwiro_public_key' );
delete_option( 'kwiro_dashboard_token' );
delete_option( 'kwiro_user_consented' );
delete_option( 'kwiro_register_claim_state' );

// Remove all transients (including JWT token with cloud API access).
delete_transient( 'kwiro_activated' );
delete_transient( 'kwiro_connection_status' );
delete_transient( 'kwiro_sync_progress' );
delete_transient( 'kwiro_jwt_token' );
delete_transient( 'kwiro_sync_lock' );
delete_transient( 'kwiro_register_backoff' );
delete_transient( 'kwiro_link_status' );

// Clear ALL scheduled CRON events (handles multiple scheduled instances).
wp_unschedule_hook( 'kwiro_batch_sync' );
wp_unschedule_hook( 'kwiro_weekly_full_sync' );

// Belt-and-suspenders: clean up any v1.1.x legacy options/transients/cron
// hooks that an interrupted v1.2 migration might have left behind. No-op
// for fresh v1.2+ installs (the keys won't exist) and idempotent.
delete_option( 'aisales_api_key' );
delete_option( 'aisales_store_id' );
delete_option( 'aisales_settings' );
delete_option( 'aisales_sync_status' );
delete_option( 'aisales_version' );
delete_option( 'aisales_public_key' );
delete_option( 'aisales_dashboard_token' );
delete_option( 'aisales_user_consented' );
delete_transient( 'aisales_activated' );
delete_transient( 'aisales_connection_status' );
delete_transient( 'aisales_sync_progress' );
delete_transient( 'aisales_jwt_token' );
delete_transient( 'aisales_sync_lock' );
delete_transient( 'aisales_register_backoff' );
delete_transient( 'aisales_link_status' );
wp_unschedule_hook( 'aisales_batch_sync' );
wp_unschedule_hook( 'aisales_weekly_full_sync' );
