<?php
/**
 * Plugin Deactivation Class
 *
 * @package SHOPEXPLORER\Inc
 * @since 1.0.0
 */

namespace SHOPEXPLORER\Inc;

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

/**
 * Deactivation class
 *
 * @since 1.0.0
 */
class SHOPEX_Deactivate {

	/**
	 * Deactivate the plugin
	 *
	 * @since 1.0.0
	 */
	public static function deactivate() {
		// Clear scheduled cron jobs
		self::clear_cron_jobs();

		// Clear any cached data
		self::clear_cache();

		// Flush rewrite rules
		flush_rewrite_rules();
	}

	/**
	 * Clear scheduled cron jobs
	 *
	 * @since 1.0.0
	 */
	private static function clear_cron_jobs() {
		// Clear sync cron job
		$sync_timestamp = wp_next_scheduled( 'shopex_sync_cron' );
		if ( $sync_timestamp ) {
			wp_unschedule_event( $sync_timestamp, 'shopex_sync_cron' );
		}

		// Clear cleanup cron job
		$cleanup_timestamp = wp_next_scheduled( 'shopex_cleanup_cron' );
		if ( $cleanup_timestamp ) {
			wp_unschedule_event( $cleanup_timestamp, 'shopex_cleanup_cron' );
		}
	}

	/**
	 * Clear cached data
	 *
	 * @since 1.0.0
	 */
	private static function clear_cache() {
		// Clear WordPress object cache
		wp_cache_flush();

		// Clear any plugin-specific transients
		global $wpdb;
		$wpdb->query(
			"DELETE FROM {$wpdb->options} 
			WHERE option_name LIKE '_transient_shopex_%' 
			OR option_name LIKE '_transient_timeout_shopex_%'"
		);
	}
}
