<?php
/**
 * Plugin deactivator.
 *
 * Runs on plugin deactivation to clean up temporary data.
 *
 * @package Antimanual_Builder
 * @since   1.0.0
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class AMBLD_Deactivator
 *
 * Handles all plugin deactivation tasks.
 *
 * @since 1.0.0
 */
class AMBLD_Deactivator {

	/**
	 * Run deactivation tasks.
	 *
	 * Flushes rewrite rules and cleans up transients.
	 * Does NOT delete user data or settings — that's handled by uninstall.php.
	 *
	 * @since  1.0.0
	 * @return void
	 */
	public static function deactivate() {
		// Flush rewrite rules.
		flush_rewrite_rules();

		// Clean up any transients.
		self::clean_transients();

		// Unschedule the RAG seed-embedding cron; it would otherwise keep
		// firing (and erroring) while the plugin is deactivated.
		wp_clear_scheduled_hook( 'ambld_seed_embed_run' );
	}

	/**
	 * Delete plugin transients.
	 *
	 * @since  1.0.0
	 * @return void
	 */
	private static function clean_transients() {
		global $wpdb;

		// Delete all transients with our prefix.
		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
				'%_transient_ambld_%',
				'%_transient_timeout_ambld_%'
			)
		);
	}
}
