<?php
/**
 * Deactivation routine.
 *
 * @package TextTango
 */

declare( strict_types=1 );

namespace TextTango\Install;

defined( 'ABSPATH' ) || exit;

/**
 * Runs on plugin deactivation.
 *
 * Deactivation is not uninstallation: it stops scheduled work but must not
 * touch settings, the delivery log, or user data.
 */
final class Deactivator {

	/**
	 * Action Scheduler hooks the plugin owns.
	 *
	 * Every hook the plugin ever schedules has to be listed, or deactivating leaves
	 * a job behind that fires against a plugin no longer there. `HookCoverageTest`
	 * holds this list against the classes that own the hooks, because the failure
	 * is silent and only shows up long after the omission.
	 *
	 * @var string[]
	 */
	public const SCHEDULED_HOOKS = array(
		'texttango_send_message',
		'texttango_reconcile_pending',
		'texttango_sync_contact',
		'texttango_reconcile_contacts',
		'texttango_request_optin',
		'texttango_purge_log',
		'texttango_check_balance',
		'texttango_refresh_campaigns',
		'texttango_abandoned_cart',
	);

	/**
	 * Entry point for `register_deactivation_hook`.
	 */
	public static function deactivate(): void {
		self::unschedule_all();
	}

	/**
	 * Cancel every pending action the plugin scheduled.
	 */
	public static function unschedule_all(): void {
		if ( ! function_exists( 'as_unschedule_all_actions' ) ) {
			return;
		}

		foreach ( self::SCHEDULED_HOOKS as $hook ) {
			as_unschedule_all_actions( $hook, array(), 'texttango' );
		}
	}
}
