<?php
/**
 * Plugin activation.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Bootstrap;

use ScheduledChanges\Capabilities\CapabilityManager;
use ScheduledChanges\Database\Migrator;
use ScheduledChanges\Support\Requirements;

/**
 * Performs bounded, repeatable activation work.
 */
final class Activator {
	/**
	 * Activates the plugin.
	 */
	public static function activate(): void {
		$requirements = new Requirements();
		$errors       = $requirements->errors();

		if ( array() !== $errors ) {
			deactivate_plugins( SCHEDULED_CHANGES_PLUGIN_BASENAME );

			wp_die(
				wp_kses_post( implode( '<br>', array_map( 'esc_html', $errors ) ) ),
				esc_html__( 'JJ\'s Post Scheduler activation failed', 'jjs-post-scheduler' ),
				array( 'back_link' => true )
			);
		}

		( new CapabilityManager() )->install();
		( new Migrator() )->migrate();

		update_option( 'scheduled_changes_version', SCHEDULED_CHANGES_VERSION, false );

		if ( false === get_option( 'scheduled_changes_activated_at', false ) ) {
			add_option( 'scheduled_changes_activated_at', gmdate( 'c' ), '', false );
		}
	}
}
