<?php
/**
 * Plugin deactivation handler.
 *
 * @package Donatico
 */

declare(strict_types=1);

namespace Donatico;

/**
 * Runs once when the plugin is deactivated via the WP admin.
 * Clears scheduled events and flushes rewrite rules.
 * Does NOT delete options or database tables — that is reserved for uninstall.php.
 */
class Deactivator {

	/**
	 * Run all deactivation routines.
	 *
	 * Called by register_deactivation_hook() in the bootstrap file.
	 *
	 * @return void
	 */
	public static function deactivate(): void {
		self::clearScheduledEvents();
		flush_rewrite_rules();
	}

	/**
	 * Remove all WP-Cron events registered by this plugin.
	 *
	 * @return void
	 */
	private static function clearScheduledEvents(): void {
		$events = array(
			'donatico_daily_cleanup',
		);

		foreach ( $events as $event ) {
			$timestamp = wp_next_scheduled( $event );
			if ( $timestamp ) {
				wp_unschedule_event( $timestamp, $event );
			}
		}
	}
}
