<?php
/**
 * Deactivation tasks.
 *
 * @package CertaCart_EU_Product_Readiness
 */

namespace CertaCart\EU_Product_Readiness;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

final class Deactivator {
	public static function deactivate( bool $network_wide = false ): void {
		if ( is_multisite() && $network_wide ) {
			$site_ids = get_sites( array( 'fields' => 'ids', 'number' => 0 ) );
			foreach ( $site_ids as $site_id ) {
				switch_to_blog( (int) $site_id );
				self::deactivate_site();
				restore_current_blog();
			}
			return;
		}
		self::deactivate_site();
	}

	private static function deactivate_site(): void {
		wp_clear_scheduled_hook( Cleanup::HOOK );
		$premium_scan_id = absint( get_option( 'ccpr_pro_active_scheduled_scan', 0 ) );
		wp_clear_scheduled_hook( 'ccpr_pro_scheduled_scan' );
		if ( $premium_scan_id ) {
			wp_clear_scheduled_hook( 'ccpr_pro_process_scan', array( $premium_scan_id ) );
		}
		wp_clear_scheduled_hook( 'ccpr_pro_process_scan' );
		if ( $premium_scan_id && function_exists( 'as_unschedule_all_actions' ) ) {
			as_unschedule_all_actions( 'ccpr_pro_process_scan', array( $premium_scan_id ), 'certacart' );
		}
		delete_option( 'ccpr_pro_active_scheduled_scan' );
	}
}
