<?php
/**
 * Fired when the plugin is deleted.
 *
 * This file is responsible for cleaning up the database and removing all traces
 * of the plugin. It executes only when the user clicks "Delete"
 * from the WordPress plugins dashboard.
 *
 * @package           smart-convert-currency-unit-conversion
 * @author            Petr Novák
 * @license           GPL-2.0-or-later
 * @link              https://petrnovak.cz/plugins/
 */

/**
 * Security Check: Ensure the uninstallation is triggered by WordPress.
 */
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}

/**
 * Global Database Access.
 */
global $wpdb;

/**
 * 1. DROP CUSTOM TABLES
 * -----------------------------------------------------------------------------
 * Remove the custom tables created by the plugin for storing currencies, units,
 * and templates.
 *
 * Direct SQL queries are necessary here because WP API does not support DROP TABLE.
 * Note: $wpdb->prepare() cannot be used on table names.
 */
$smart_convert_currency_unit_conversion_table_currency  = $wpdb->prefix . 'smart_convert_currency_unit_conversion_currencies';
$smart_convert_currency_unit_conversion_table_units     = $wpdb->prefix . 'smart_convert_currency_unit_conversion_units';
$smart_convert_currency_unit_conversion_table_templates = $wpdb->prefix . 'smart_convert_currency_unit_conversion_templates';

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
$wpdb->query( "DROP TABLE IF EXISTS $smart_convert_currency_unit_conversion_table_currency" );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
$wpdb->query( "DROP TABLE IF EXISTS $smart_convert_currency_unit_conversion_table_units" );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
$wpdb->query( "DROP TABLE IF EXISTS $smart_convert_currency_unit_conversion_table_templates" );

/**
 * 2. DELETE SPECIFIC OPTIONS
 * -----------------------------------------------------------------------------
 * Remove known main settings.
 */
delete_option( 'smart_convert_currency_unit_conversion_option_name' );
delete_site_option( 'smart_convert_currency_unit_conversion_option_name' );
delete_option( 'smart_convert_currency_unit_conversion_last_rates_update_time' );

/**
 * 3. ADVANCED CLEANUP (WILDCARD DELETE)
 * -----------------------------------------------------------------------------
 * This is the critical fix. Instead of deleting transients one by one,
 * we remove ALL options and transients that start with the plugin's prefixes.
 * This catches:
 * - Dynamic transients (e.g., cached calculations)
 * - Forgotten flags
 */

// Prefixes to look for in the 'option_name' column
$smart_convert_currency_unit_conversion_prefixes = array(
	'smart_convert_currency_unit_conversion_%',                        // Main short prefix (e.g. SMART_CONVERT_VERSION)
	'smart_convert_currency_unit_conversion_%',         // Full slug prefix
	'_transient_smart_convert_currency_unit_conversion_%',             // Transients data
	'_transient_timeout_smart_convert_currency_unit_conversion_%',     // Transients expiration
	'_transient_smart_convert_%',   // Long prefix transients
	'_transient_timeout_smart_convert_%',
);

foreach ( $smart_convert_currency_unit_conversion_prefixes as $smart_convert_currency_unit_conversion_prefix ) {
	// We use $wpdb->prepare for safety, even though the prefix is an internal string.
	// Direct query is needed for wildcard DELETE.
	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
	$wpdb->query(
		$wpdb->prepare(
			"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
			$smart_convert_currency_unit_conversion_prefix
		)
	);
}

/**
 * 4. CLEAR SCHEDULED CRON JOBS
 * -----------------------------------------------------------------------------
 * Unschedule the cron event for currency updates.
 */
$smart_convert_currency_unit_conversion_timestamp = wp_next_scheduled( 'smart_convert_currency_unit_conversion_update_currency_hook' );
if ( $smart_convert_currency_unit_conversion_timestamp ) {
	wp_unschedule_event( $smart_convert_currency_unit_conversion_timestamp, 'smart_convert_currency_unit_conversion_update_currency_hook' );
}
// Clear hook explicitly to be safe.
wp_clear_scheduled_hook( 'smart_convert_currency_unit_conversion_update_currency_hook' );