<?php
/**
 * WC Affiliate Uninstall
 *
 * Fires when the plugin is deleted (not deactivated) from the WordPress plugins screen.
 * Cleans up all plugin options, transients, and database tables.
 *
 * @package WC_Affiliate
 */

defined( 'WP_UNINSTALL_PLUGIN' ) || exit;

// Remove all plugin options.
$wca_options = array(
	'wc_affiliate_db_version',
	'wc_affiliate_activated',
	'wc_affiliate_settings',
	'wc_affiliate_wizard_data',
	'wc_affiliate_wizard_completed',
	'wc_affiliate_plugin_user_status',
	'wc_affiliate_plugin_migration_status',
	'wc_affiliate_migration_notice_dismissed',
	'wc_affiliate_settings_migrated',
	'wc_affiliate_rankings_monthly',
	'wc_affiliate_rankings_quarterly',
	'wc_affiliate_rankings_yearly',
	'wc_affiliate_settings_page_reloaded',
	'wc-affiliate_setup',
);

foreach ( $wca_options as $wca_option ) {
	delete_option( $wca_option );
}

// Remove stale export job options (dynamic keys).
global $wpdb;
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'wc_affiliate_export_job_%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery

// Remove plugin transients.
delete_transient( 'wc_affiliate_activation_redirect' );
delete_transient( 'affiliate_role_assigned' );

// Drop plugin tables.
$wca_tables = array(
	$wpdb->prefix . 'wca_visits',
	$wpdb->prefix . 'wca_referrals',
	$wpdb->prefix . 'wca_transactions',
	$wpdb->prefix . 'wca_shortlinks',
);

foreach ( $wca_tables as $wca_table ) {
	$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wca_table ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
}
