<?php
/**
 * Shared, idempotent uninstall cleanup for Quick Variable Products.
 *
 * The free build calls this from uninstall.php (WordPress runs it standalone).
 * The pro build ships no uninstall.php (the Freemius deployment processor rejects
 * one) and calls this from the SDK's after_uninstall hook instead. It removes the
 * plugin's OWN options, per-user preferences and bounded history data — including
 * any left behind by the Pro tier, so a downgrade-then-uninstall leaves nothing.
 * Created products, attributes, categories, tags and media are the store's
 * content and are intentionally left untouched.
 *
 * @package ErenLabs\QuickVariableProducts
 */

defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'erenqvp_uninstall_cleanup' ) ) {
	/**
	 * Delete every option / user-meta / transient this plugin owns.
	 *
	 * @return void
	 */
	function erenqvp_uninstall_cleanup() {
		// Free options + per-user preferences and onboarding flags.
		delete_option( 'erenqvp_sku_prefix' );
		delete_option( 'erenqvp_auto_sku_default' );
		delete_metadata( 'user', 0, 'erenqvp_last_prefs', '', true );
		delete_metadata( 'user', 0, 'erenqvp_tour_done', '', true );
		delete_metadata( 'user', 0, 'erenqvp_tour_bulk', '', true );
		delete_metadata( 'user', 0, 'erenqvp_tour_existing', '', true );
		delete_metadata( 'user', 0, 'erenqvp_create_count', '', true );
		delete_metadata( 'user', 0, 'erenqvp_review_ask_done', '', true );
		delete_metadata( 'user', 0, 'erenqvp_locale', '', true );
		delete_transient( 'erenqvp_activation_redirect' );

		// Pro tier data (harmless no-ops on a free-only store).
		delete_option( 'erenqvpp_templates' );
		delete_metadata( 'user', 0, 'erenqvpp_last_run', '', true );
		delete_metadata( 'user', 0, 'erenqvpp_multi_runs', '', true );
		delete_metadata( 'user', 0, 'erenqvpp_bulk_undo', '', true );
		delete_metadata( 'user', 0, 'erenqvpp_history_index', '', true );
		delete_metadata( 'user', 0, 'erenqvpp_bulk_recipes', '', true );

		global $wpdb;

		// History snapshots use bounded, dynamic per-run meta keys; remove only our prefix.
		$erenqvpp_history_like = $wpdb->esc_like( 'erenqvpp_history_run_' ) . '%';
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall cleanup has no cacheable API for wildcard user-meta keys.
		$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE %s", $erenqvpp_history_like ) );

		$erenqvpp_lock_like          = $wpdb->esc_like( 'erenqvpp_hlock_' ) . '%';
		$erenqvpp_batch_like         = $wpdb->esc_like( '_transient_erenqvpp_batch_' ) . '%';
		$erenqvpp_batch_timeout_like = $wpdb->esc_like( '_transient_timeout_erenqvpp_batch_' ) . '%';
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall cleanup for bounded dynamic locks/transients.
		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s",
				$erenqvpp_lock_like,
				$erenqvpp_batch_like,
				$erenqvpp_batch_timeout_like
			)
		);
	}
}
