<?php
/**
 * Shared, idempotent uninstall cleanup for Smart Categories for WooCommerce.
 *
 * 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 tables, term meta, options and scheduled jobs — including anything
 * the Pro tier left behind, so a downgrade-then-uninstall leaves nothing. Products
 * remain in whatever categories they were assigned to (that is store content).
 *
 * @package ErenLabs\SmartCategories
 */

defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'scwc_uninstall_cleanup' ) ) {
	/**
	 * Remove every table / term-meta / option / cron job this plugin owns.
	 * Multisite-aware and safe to run more than once.
	 *
	 * @return void
	 */
	function scwc_uninstall_cleanup() {
		$run = static function () {
			global $wpdb;

			// Custom tables — %i identifier placeholder (WP 6.2+), zero concatenation.
			foreach ( array( $wpdb->prefix . 'scwc_assignments', $wpdb->prefix . 'scwc_sales' ) as $scwc_table ) {
				// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- Uninstall-scope table drop.
				$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $scwc_table ) );
			}

			// Plugin term meta (fixed keys).
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall-scope meta delete.
			$wpdb->query( "DELETE FROM {$wpdb->termmeta} WHERE meta_key IN ( '_scwc_rules', '_scwc_enabled', '_scwc_sort', '_scwc_hier' )" );

			// Engine options.
			foreach ( array( 'scwc_db_version', 'scwc_sales_last_run', 'scwc_refresh_interval', 'scwc_activated_at', 'scwc_review' ) as $scwc_opt ) {
				delete_option( $scwc_opt );
			}

			// Factory-core options (migration ledger + locks live under the slug prefix).
			$scwc_core_like = $wpdb->esc_like( 'smart_categories_for_woocommerce_' ) . '%';
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall-scope prefix delete.
			$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $scwc_core_like ) );

			// Scheduled background jobs (Action Scheduler + any prefixed cron).
			foreach ( array( 'scwc_process_term_page', 'scwc_process_page', 'scwc_process_term', 'scwc_aggregate_sales', 'scwc_daily_refresh' ) as $scwc_hook ) {
				if ( function_exists( 'as_unschedule_all_actions' ) ) {
					as_unschedule_all_actions( $scwc_hook );
				}
				wp_clear_scheduled_hook( $scwc_hook );
			}

			wp_cache_flush(); // The direct deletes bypassed the options/meta caches.
		};

		if ( is_multisite() ) {
			foreach ( get_sites( array( 'fields' => 'ids', 'number' => 0 ) ) as $scwc_site_id ) {
				switch_to_blog( (int) $scwc_site_id );
				$run();
				restore_current_blog();
			}
		} else {
			$run();
		}
	}
}
