<?php
/**
 * Plugin activation and deactivation routines.
 *
 * @package WP_PowerSuite\Core
 * @since 1.0.0
 */

namespace WP_PowerSuite\Core;

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

/**
 * Runs install-time setup and teardown for WP PowerSuite.
 *
 * ## Multisite behavior
 *
 * The plugin header declares `Network: true`, so it may be network-activated or
 * activated per site.
 *
 * **Single site:** Activation runs once. Settings, version, and per-blog options
 * live in `wp_options`. Core crons are registered for that site.
 *
 * **Multisite — per-site activation:** Each blog that activates the plugin runs
 * this hook independently. {@see NetworkScope::usesNetworkStorage()} is false, so
 * settings and version are stored in that blog's `wp_options`. Subsites do not
 * share configuration unless the plugin is network-activated.
 *
 * **Multisite — network activation:** WordPress runs the activation callback once
 * per site in the network (switching blogs internally). After network activation,
 * {@see NetworkScope::usesNetworkStorage()} is true, so shared settings and the
 * version option are stored in `wp_sitemeta` via site options. The admin UI is
 * registered under Network Admin. Each site still receives flushed caches and
 * scheduled core crons in its own cron table.
 *
	 * **Deactivation** clears all plugin and module cron hooks ({@see DataRegistry::getCronHooks()}).
	 * Module settings and data are left intact so reactivation is non-destructive.
 */
final class PluginLifecycle {

	/**
	 * Run activation tasks for the current site context.
	 */
	public static function activate(): void {
		OptionStore::getInstance()->seedDefaultsIfMissing();
		if ( defined( 'WP_POWERSUITE_VERSION' ) ) {
			PluginUpgrader::set_version( WP_POWERSUITE_VERSION );
		}
		PluginUpgrader::flush_caches();
		self::scheduleCoreCrons();
		OptionAutoloadMigration::run();

		/**
		 * Fires after WP PowerSuite activation tasks complete for the current site.
		 *
		 * @since 1.0.0
		 */
		do_action( 'wp_powersuite_activated' );
	}

	/**
	 * Run deactivation tasks for the current site context.
	 */
	public static function deactivate(): void {
		self::unschedulePluginCrons();

		/**
		 * Fires after WP PowerSuite deactivation tasks complete for the current site.
		 *
		 * @since 1.0.0
		 */
		do_action( 'wp_powersuite_deactivated' );
	}

	/**
	 * Register cron events owned by the plugin core (not individual modules).
	 */
	private static function scheduleCoreCrons(): void {
		foreach ( DataRegistry::getCoreCronHooks() as $hook ) {
			if ( wp_next_scheduled( $hook ) ) {
				continue;
			}

			if ( $hook === 'wp_powersuite_license_recheck' ) {
				wp_schedule_event( time() + HOUR_IN_SECONDS, 'twicedaily', $hook );
			}
		}
	}

	/**
	 * Remove all plugin cron events so they do not fire while the plugin is inactive.
	 */
	private static function unschedulePluginCrons(): void {
		foreach ( DataRegistry::getCronHooks() as $hook ) {
			wp_clear_scheduled_hook( $hook );
		}
	}
}
