<?php
/**
 * Activation routine.
 *
 * @package MenuVerse
 */

namespace WPXero\MenuVerse;

use WPXero\MenuVerse\Settings\Settings;

defined( 'ABSPATH' ) || exit;

/**
 * Prepares capabilities and baseline options on activation.
 */
final class Activator {

	/**
	 * Runs on plugin activation.
	 *
	 * @return void
	 */
	public static function activate() {
		Capabilities::add_caps();

		if ( false === get_option( Settings::OPTION, false ) ) {
			// Modules are not registered this early, so only the container is
			// seeded; per module defaults are filled in on first read.
			add_option(
				Settings::OPTION,
				array(
					'_version' => MENUVERSE_VERSION,
					'global'   => array(),
					'modules'  => array(),
				),
				'',
				true
			);
		}

		$state = get_option( Settings::STATE_OPTION, array() );
		$state = is_array( $state ) ? $state : array();

		if ( empty( $state['installed_at'] ) ) {
			$state['installed_at'] = time();
			$state['onboarded']    = false;
		}

		$state['version'] = MENUVERSE_VERSION;

		update_option( Settings::STATE_OPTION, $state, false );

		/**
		 * Fires after MenuVerse has been activated.
		 *
		 * @since 1.0.0
		 */
		do_action( 'menuverse_activated' );
	}
}
