<?php
/**
 * Handles plugin activation related tasks.
 *
 * @since      1.0.0
 * @package    Author_Kit
 * @subpackage Author_Kit/includes
 */

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

/**
 * Handles plugin activation tasks.
 *
 * @since      1.0.0
 * @package    Author_Kit
 * @subpackage Author_Kit/includes
 */
class Author_Kit_Activator {

	/**
	 * Runs on plugin activation.
	 *
	 * @since 1.0.0
	 */
	public static function activate() {

		self::multisite_compatibility();

		// Will auto-deactivate and die if incompatible.
		self::compatibility_check();

		// Create the coauthor table upon plugin activation.
		$author_kit_coauthors = new Author_Kit_Coauthors();
		$author_kit_coauthors->create_coauthor_table();

		// Update user online status.
		self::author_kit_set_users_online_status();

		self::add_guest_role();
		self::author_kit_add_custom_user_capability();

		// Save the values for
		// Supported platforms with metadata.
		$default_social_platforms = array(

			'facebook',
			'twitter',
			'square-x-twitter',
			'linkedin',
			'instagram',
			'youtube',
			'github',
			'pinterest',
			'tiktok',
			'snapchat',
			'whatsapp',
			'telegram',
			'discord',
			'twitch',
			'medium',
			'bluesky',
			'mastodon',
			'threads',
		);

		// Default plugin options.
		$options_to_save = array(
			'author_kit_author_bio_template'      => 'template-default',
			'author_kit_author_archive_template'  => 'archive-template-default',
			'author_kit_custom_byline_template'   => 'byline-default',
			'author_kit_list_all_author_template' => 'list-all-author-default',
			'author_kit_bio_display'              => true,
			'author_kit_archive_display'          => true,
			'author_kit_byline_display'           => false,
			'author_kit_selected_platforms'       => $default_social_platforms,
		);

		foreach ( $options_to_save as $key => $value ) {
			if ( get_option( $key ) === false ) {
				add_option( $key, $value );
			}
		}

		Author_Kit_Social_Profile::get_selected_platforms_with_meta();
		author_kit_update_compatibility_status();
	}

	/**
	 * Don't allow avtivation on multisite websites
	 *
	 * @return void
	 */
	private static function multisite_compatibility() {
		$error = null;

		if ( is_multisite() ) {
			$error = new WP_Error(
				'author_kit_multisite_activation_error',
				esc_html__( 'Author Kit is not compatible with WordPress Multisite. It will be deactivated automatically', 'author-kit' ),
			);
		}

		if ( is_wp_error( $error ) ) {
			if ( function_exists( 'deactivate_plugins' ) ) {
				deactivate_plugins( plugin_basename( AUTHOR_KIT_PLUGIN_FILE ) );
			}

			wp_die(
				esc_html( $error->get_error_message() ),
				esc_html__( 'Plugin Activation Error', 'author-kit' ),
				array( 'back_link' => true )
			);
		}
	}


	/**
	 * Checks WordPress and PHP version compatibility.
	 * Deactivates plugin and dies with error if not compatible.
	 *
	 * @return void
	 */
	public static function compatibility_check() {
		global $wp_version;

		$required_php = '7.4';
		$required_wp  = '5.4';

		$error = null;

		if ( version_compare( PHP_VERSION, $required_php, '<' ) ) {
			$error = new WP_Error(
				'author_kit_php_version',
				sprintf(
					// translators: %1$s is minimum PHP Version Required, %2$s is the current PHP version of the user.
					esc_html__( 'Plugin Activation Error: Author Kit requires PHP version %1$s or higher. You are running %2$s.', 'author-kit' ),
					$required_php,
					PHP_VERSION
				)
			);
		} elseif ( version_compare( $wp_version, $required_wp, '<' ) ) {
			$error = new WP_Error(
				'author_kit_wp_version',
				sprintf(
					// translators: %1$s is minimum WordPress Version Required, %2$s is the current WordPress version of the user.
					esc_html__( 'Plugin Activation Error: Author Kit requires WordPress version %1$s or higher. You are running %2$s.', 'author-kit' ),
					$required_wp,
					$wp_version
				)
			);
		}

		if ( is_wp_error( $error ) ) {
			if ( function_exists( 'deactivate_plugins' ) ) {
				deactivate_plugins( plugin_basename( AUTHOR_KIT_PLUGIN_FILE ) );
			}

			wp_die(
				esc_html( $error->get_error_message() ),
				esc_html__( 'Plugin Activation Error', 'author-kit' ),
				array( 'back_link' => true )
			);
		}
	}



	/**
	 * Set the status of a current user to "Online" upon activation
	 *
	 * @return void
	 */
	public static function author_kit_set_users_online_status() {

		$user_id = get_current_user_id();

		// Retrieve the list of online users from the transient cache; initialize as empty array if not set.
		$online_users = get_transient( 'author_kit_online_users' );
		$online_users = is_array( $online_users ) ? $online_users : array();

		// Update the current user's last active timestamp.
		$online_users[ $user_id ] = time();
		set_transient( 'author_kit_online_users', $online_users, 60 ); // Store for 1 minute.
	}

	/**
	 * Add the custom "Guest" role upon activation.
	 *
	 * @return void
	 */
	public static function add_guest_role() {
		add_role(
			'guest', // Role identifier.
			__( 'Guest', 'author-kit' ), // Display name.
			array(
				'read'          => true, // Allows reading the site.
				'edit_posts'    => true, // Allows editing posts.
				'publish_posts' => false, // Allows publishing posts.
				'delete_posts'  => false, // Prevents deleting posts.
				'upload_files'  => true, // Allows uploading files (if needed).
			)
		);
	}

	/**
	 * Add the custom "author_kit_add_edit_user" capability upon activation.
	 *
	 * @return void
	 */
	public static function author_kit_add_custom_user_capability() {
		$role = get_role( 'administrator' );
		if ( $role ) {
			$role->add_cap( 'author_kit_add_edit_user' );
		}
	}
}
