<?php
/**
 * Fired during plugin activation
 *
 * @link    https://personalizewp.com
 * @since   1.0.0
 *
 * @package PersonalizeWP
 */

namespace PersonalizeWP;

use PersonalizeWP\DB_Manager;
use PersonalizeWP\Admin\PageSettings;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 */
class Activator {

	/**
	 * Initialise the plugin after activation
	 */
	public static function activate() {
		add_action(
			'activated_plugin',
			function () {

				// Check if the database structure needs installing.
				DB_Manager::instance()->check_install();

				// Trigger the onboarding wizard to appear, if not already.
				// Uses add_option as doesn't override if option already exists.
				add_option( 'pwp_pending_onboarding', true, '', true );

				// Re-autoload options
				if ( function_exists( 'wp_set_option_autoload_values' ) ) {
					wp_set_options_autoload(
						[
							'_pwp_db_version',
							'personalizewp',
							'pwp_admin_onboarding_dismissed',
							'pwp_pending_onboarding',
						],
						true
					);
				}

				$wp_version = get_bloginfo( 'version' );
				// Check if activated via the short lived AJAX modal, thus no redirect possible.
				if ( version_compare( $wp_version, '6.5', '>=' ) && version_compare( $wp_version, '6.5.4', '<' ) && wp_doing_ajax() ) {
					return;
				}

				// Only apply auto-redirect if not already onboarded (this is set to false upon completion)
				if ( ! get_option( 'pwp_pending_onboarding' ) ) {
					return;
				}

				// Skip redirect when activating via CLI.
				if ( defined( 'WP_CLI' ) && WP_CLI ) {
					return;
				}

				$settings_page = admin_url(
					add_query_arg(
						array(
							'page' => PageSettings::SLUG,
						),
						'admin.php'
					)
				);
				wp_safe_redirect( $settings_page );
				exit;
			}
		);
	}
}
