<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and  hooks to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Author_Kit
 * @subpackage Author_Kit/admin
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Class Author_Kit_Admin
 *
 * Handles all admin-specific functionality for the Author Kit plugin.
 *
 * This class defines the plugin name, version, and registers all
 * hooks related to the admin area, including enqueuing stylesheets
 * and scripts, adding meta boxes, and other admin-specific features.
 */
class Author_Kit_Admin {


	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string $plugin_name       The name of this plugin.
	 * @param      string $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version     = $version;

		require_once AUTHOR_KIT_PLUGIN_PATH . 'includes/core/class-author-kit-update-author-profile.php';
		require_once AUTHOR_KIT_PLUGIN_PATH . 'includes/core/class-author-kit-user-table.php';
		require_once AUTHOR_KIT_PLUGIN_PATH . 'includes/core/class-author-kit-post-table.php';
		require_once AUTHOR_KIT_PLUGIN_PATH . 'includes/core/class-author-kit-profile-table.php';
		require_once AUTHOR_KIT_PLUGIN_PATH . 'includes/core/class-author-kit-user-functionality.php';
		require_once AUTHOR_KIT_PLUGIN_PATH . 'includes/core/class-author-kit-help-email.php';
		require_once AUTHOR_KIT_PLUGIN_PATH . 'includes/core/class-author-kit-load-kirki-pro.php';

		require_once AUTHOR_KIT_PLUGIN_PATH . 'includes/core/functions.php';

		// file to switch active template related to author-kit-template page.
		require_once AUTHOR_KIT_PLUGIN_PATH . 'includes/core/class-author-kit-template-activation.php';
		require_once AUTHOR_KIT_PLUGIN_PATH . 'includes/core/class-author-kit-byline-manager.php';

		require_once AUTHOR_KIT_PLUGIN_PATH . 'includes/class/class-author-kit-coauthors.php';
		require_once AUTHOR_KIT_PLUGIN_PATH . 'includes/class/class-author-kit-block-user.php';
		require_once AUTHOR_KIT_PLUGIN_PATH . 'includes/class/class-author-kit-online-user.php';
	}

	/**
	 * Enqueue admin-specific stylesheets based on the current admin page hook.
	 *
	 * Loads different CSS files conditionally for various Author Kit admin pages,
	 * including support for RTL styles and global admin styles.
	 *
	 * @param string $hook The current admin page hook suffix.
	 *
	 * @return void
	 */
	public function enqueue_styles( $hook ) {

		/**
		 * An instance of this class should be passed to the run() function
		 * defined in Author_Kit_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Author_Kit_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 *
		 * Load Customizer scripts only in Customizer
		 */

		if ( is_rtl() && author_kit_admin_page() ) {
			wp_enqueue_style( 'author-kit-rtl-css', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/css/author-kit-rtl-common-style.css', array(), $this->version, 'all' );
		}

		// Global Enqueing for author ki admin pages.
		if ( author_kit_admin_page() ) {
			wp_enqueue_style( 'author-kit-icons-css', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/fontawesome/css/all.css', array(), $this->version, 'all' );
			wp_enqueue_style( 'author-kit-common-css', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/css/author-kit-common.css', array(), $this->version, 'all' );

		}

		if ( author_kit_admin_page() ) {
			wp_enqueue_style( 'author-kit-admin-header', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/css/author-kit-admin-header.css', array(), $this->version, 'all' );
		}

		if ( 'toplevel_page_author-kit-dashboard' === $hook ) {
			wp_enqueue_style( 'author-kit-admin-dashboard', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/css/author-kit-dashboard.css', array(), $this->version, 'all' );
		}

		if ( 'author-kit_page_author-kit-posts' === $hook ) {
			wp_enqueue_style( 'author-kit-post-table', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/css/author-kit-post-table.css', array(), $this->version, 'all' );
		}

		if ( 'author-kit_page_author-kit-author-profiles' === $hook ) {
			wp_enqueue_style( 'author-kit-profile-table', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/css/author-kit-profile-table.css', array(), $this->version, 'all' );
		}

		if ( 'author-kit_page_author-kit-templates' === $hook ) {
			wp_enqueue_style( 'author-kit-templates', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/css/author-kit-templates.css', array(), $this->version, 'all' );
		}

		if ( 'author-kit_page_author-kit-settings' === $hook ) {
			wp_enqueue_style( 'author-kit-settings', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/css/author-kit-settings.css', array(), $this->version, 'all' );
		}

		if ( 'author-kit_page_author-kit-add-new-user' === $hook || 'admin_page_author-kit-add-new-user' === $hook ) {
			wp_enqueue_style( 'author-kit-add-new-user', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/css/author-kit-user.css', array(), $this->version, 'all' );
			wp_enqueue_style( 'author-kit-update-author-profile', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/css/author-kit-update-author-profile.css', array(), $this->version, 'all' );
		}

		if ( 'author-kit_page_author-kit-edit-user' === $hook || 'admin_page_author-kit-edit-user' === $hook ) {
			wp_enqueue_style( 'author-kit-add-new-user', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/css/author-kit-user.css', array(), $this->version, 'all' );
			wp_enqueue_style( 'author-kit-update-author-profile', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/css/author-kit-update-author-profile.css', array(), $this->version, 'all' );
		}

		if ( 'author-kit_page_author-kit-update-author-profile' === $hook || 'admin_page_author-kit-update-author-profile' === $hook ) {
			wp_enqueue_style( 'author-kit-update-author-profile', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/css/author-kit-update-author-profile.css', array(), $this->version, 'all' );
		}

		if ( 'post.php' === $hook || 'post-new.php' === $hook ) {
			wp_enqueue_style( 'author-kit-metabox', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/css/author-kit-metabox.css', array(), $this->version, 'all' );
		}
	}

	/**
	 * Register the JavaScript for the customizer
	 *
	 * @since    1.0.0
	 */
	public function enqueue_customizer_scripts() {
		wp_enqueue_script( 'author-kit-customizer-preview', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/js/author-kit-customizer-preview.js', array( 'jquery', 'customize-preview' ), $this->version, true );
	}

	/**
	 * Enqueue admin-specific JavaScript files and localize script variables
	 * based on the current admin page hook.
	 *
	 * Loads global scripts for admin header functionality and conditionally
	 * enqueues page-specific scripts with localized data for AJAX handling,
	 * permissions, and settings relevant to Author Kit plugin admin pages.
	 *
	 * @param string $hook The current admin page hook suffix.
	 *
	 * @return void
	 */
	public function enqueue_scripts( $hook ) {

		/**
		 * An instance of this class should be passed to the run() function
		 * defined in Author_Kit_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Author_Kit_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		// global js script for admin header and other stuff.
		if ( author_kit_admin_page() ) {
			wp_enqueue_script( 'author-kit-admin-header', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/js/author-kit-admin-header.js', array(), $this->version, false );
			wp_localize_script(
				'author-kit-admin-header',
				'author_kit_admin_header_vars',
				array(
					'author_kit_admin_url' => admin_url( 'admin.php?page=' ),
				)
			);
		}

		if ( 'toplevel_page_author-kit-dashboard' === $hook ) {
			wp_enqueue_script( 'author-kit-dashboard-script', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/js/author-kit-dashboard.js', array( 'wp-i18n' ), $this->version, false );
			wp_localize_script(
				'author-kit-dashboard-script',
				'author_kit_user_table_vars',
				array(
					'ajax_url'                    => admin_url( 'admin-ajax.php' ),
					'nonce'                       => wp_create_nonce( 'user-table-loader-nonce' ),
					'current_user_id'             => get_current_user_id(),
					'is_admin'                    => current_user_can( 'manage_options' ),
					'is_edit_users'               => current_user_can( 'edit_users' ),
					'is_delete_users'             => current_user_can( 'delete_users' ),
					'is_manage_options'           => current_user_can( 'manage_options' ),
					'edit_user_nonce'             => wp_create_nonce( 'author-kit-edit-user-nonce' ),
					'author_kit_add_new_user_url' => admin_url( 'admin.php?page=author-kit-add-new-user&add-new-user-nonce=' . wp_create_nonce( 'author-kit-add-new-user-nonce' ) ),
					'author_kit_is_create_users'  => current_user_can( 'create_users' ),
				)
			);
			wp_set_script_translations( 'author-kit-dashboard-script', 'author-kit' );
		}

		if ( 'author-kit_page_author-kit-posts' === $hook ) {
			wp_enqueue_script( 'author-kit-post-table-script', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/js/author-kit-post-table.js', array( 'wp-i18n' ), $this->version, false );

			$is_admin                  = current_user_can( 'manage_options' );
			$is_edit_posts             = current_user_can( 'edit_posts' );
			$is_edit_others_posts      = current_user_can( 'edit_others_posts' );
			$is_edit_published_posts   = current_user_can( 'edit_published_posts' );
			$is_edit_private_posts     = current_user_can( 'edit_private_posts' );
			$is_publish_posts          = current_user_can( 'publish_posts' );
			$is_delete_posts           = current_user_can( 'delete_posts' );
			$is_delete_others_posts    = current_user_can( 'delete_others_posts' );
			$is_delete_published_posts = current_user_can( 'delete_published_posts' );
			$is_delete_private_posts   = current_user_can( 'delete_private_posts' );
			$is_read_private_posts     = current_user_can( 'read_private_posts' );

			wp_localize_script(
				'author-kit-post-table-script',
				'author_kit_post_table_vars',
				array(
					'ajax_url'                  => admin_url( 'admin-ajax.php' ),
					'nonce'                     => wp_create_nonce( 'post-table-loader-nonce' ),
					'is_admin'                  => $is_admin,
					'is_edit_posts'             => $is_edit_posts,
					'is_edit_others_posts'      => $is_edit_others_posts,
					'is_edit_published_posts'   => $is_edit_published_posts,
					'is_edit_private_posts'     => $is_edit_private_posts,
					'is_publish_posts'          => $is_publish_posts,
					'is_delete_posts'           => $is_delete_posts,
					'is_delete_others_posts'    => $is_delete_others_posts,
					'is_delete_published_posts' => $is_delete_published_posts,
					'is_delete_private_posts'   => $is_delete_private_posts,
					'is_read_private_posts'     => $is_read_private_posts,
				)
			);
			wp_set_script_translations( 'author-kit-post-table-script', 'author-kit' );
		}

		if ( 'author-kit_page_author-kit-author-profiles' === $hook ) {
			wp_enqueue_script( 'author-kit-profile-table-script', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/js/author-kit-profile-table.js', array( 'wp-i18n' ), $this->version, false );
			wp_localize_script(
				'author-kit-profile-table-script',
				'author_kit_profile_table_vars',
				array(
					'ajax_url'                    => admin_url( 'admin-ajax.php' ),
					'nonce'                       => wp_create_nonce( 'author-profile-loader-nonce' ),
					'is_admin'                    => current_user_can( 'manage_options' ),
					'is_manage_options'           => current_user_can( 'manage_options' ),
					'is_edit_users'               => current_user_can( 'edit_users' ),
					'current_user_id'             => get_current_user_id(),
					'update_author_profile_nonce' => wp_create_nonce( 'author-kit-update-author-profile-nonce' ),
				)
			);
			wp_set_script_translations( 'author-kit-profile-table-script', 'author-kit' );
		}

		if ( 'author-kit_page_author-kit-templates' === $hook ) {
			wp_enqueue_script( 'author-kit-template-activation-script', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/js/author-kit-templates.js', array( 'wp-i18n' ), $this->version, false );
			wp_enqueue_script( 'jquery' );

			// Updated active template options with the correct names and default values.
			$active_templates = array(
				'author_bio'     => get_option( 'author_kit_author_bio_template', 'template-default' ),
				'author_archive' => get_option( 'author_kit_author_archive_template', 'archive-template-default' ),
				'custom_byline'  => get_option( 'author_kit_custom_byline_template', 'byline-default' ),
			);

			wp_localize_script(
				'author-kit-template-activation-script',
				'author_kit_template_activation_vars',
				array(
					'ajaxurl'         => admin_url( 'admin-ajax.php' ),
					'activeTemplates' => $active_templates, // Pass active template IDs with updated names.
					'nonce'           => wp_create_nonce( 'author_kit_template_activation' ),
				)
			);

			wp_localize_script(
				'author-kit-template-activation-script',
				'author_kit_switch_ajax_object_vars',
				array(
					'ajaxurl'       => admin_url( 'admin-ajax.php' ),
					'nonce'         => wp_create_nonce( 'author_kit_toggle_bio_display' ),
					'current_state' => array(
						'bio'     => get_option( 'author_kit_bio_display', true ),
						'archive' => get_option( 'author_kit_archive_display', false ),
						'byline'  => get_option( 'author_kit_byline_display', false ),
					),
				)
			);
		}

		if ( 'author-kit_page_author-kit-add-new-user' === $hook || 'admin_page_author-kit-add-new-user' === $hook ) {
			wp_enqueue_media(); // Enqueues the WordPress Media Library scripts.
			wp_enqueue_script( 'author-kit-add-edit-script', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/js/author-kit-user.js', array( 'jquery', 'wp-i18n' ), $this->version, false );
			wp_localize_script(
				'author-kit-add-edit-script',
				'author_kit_user_script_vars',
				array(
					'ajax_url'                => admin_url( 'admin-ajax.php' ),
					'nonce'                   => wp_create_nonce( 'author-kit-add-edit-user-nonce' ),
					'default_user_avatar_url' => AUTHOR_KIT_PLUGIN_URL . 'admin/assets/files/default_user_avatar.jpg',
				)
			);
		}

		if ( 'author-kit_page_author-kit-edit-user' === $hook || 'admin_page_author-kit-edit-user' === $hook ) {
			wp_enqueue_media(); // Enqueues the WordPress Media Library scripts.
			wp_enqueue_script( 'author-kit-add-edit-script', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/js/author-kit-user.js', array( 'jquery', 'wp-i18n' ), $this->version, false );
			wp_localize_script(
				'author-kit-add-edit-script',
				'author_kit_user_script_vars',
				array(
					'ajax_url'                => admin_url( 'admin-ajax.php' ),
					'nonce'                   => wp_create_nonce( 'author-kit-add-edit-user-nonce' ),
					'default_user_avatar_url' => AUTHOR_KIT_PLUGIN_URL . 'admin/assets/files/default_user_avatar.jpg',
				)
			);
		}

		if ( 'author-kit_page_author-kit-settings' === $hook ) {
			wp_enqueue_script(
				'author-kit-settings-js',
				AUTHOR_KIT_PLUGIN_URL . 'admin/assets/js/author-kit-settings.js',
				array( 'jquery', 'wp-i18n' ),
				$this->version,
				true
			);
			wp_localize_script(
				'author-kit-settings-js',
				'author_kit_settings_script_vars',
				array(
					'ajax_url' => admin_url( 'admin-ajax.php' ),
					'nonce'    => wp_create_nonce( 'author-kit-help-nonce' ),
				)
			);
		}

		if ( 'author-kit_page_author-kit-update-author-profile' === $hook || 'admin_page_author-kit-update-author-profile' === $hook ) {
			wp_enqueue_media();
			wp_enqueue_script( 'author-kit-update-author-profile-script', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/js/author-kit-update-author-profile.js', array( 'jquery', 'wp-i18n' ), $this->version, false );

			// Fetch selected platforms meta (icon, color, etc.) from transient; fallback to options if needed.
			$selected_platforms_meta = get_transient( 'author_kit_selected_platforms_meta' );
			if ( false === $selected_platforms_meta || ! is_array( $selected_platforms_meta ) ) {
				$selected_platforms_meta = get_option( 'author_kit_selected_platforms_meta', array() );
				$selected_platforms_meta = is_array( $selected_platforms_meta ) ? $selected_platforms_meta : array();
				// Cache for future use.
				set_transient( 'author_kit_selected_platforms_meta', $selected_platforms_meta, HOUR_IN_SECONDS );
			}

			$selected_platforms_keys = array_keys( $selected_platforms_meta );

			wp_localize_script(
				'author-kit-update-author-profile-script',
				'author_kit_update_author_profile_vars',
				array(
					'ajax_url'                => admin_url( 'admin-ajax.php' ),
					'nonce'                   => wp_create_nonce( 'author-kit-update-author-profile-nonce' ),
					'selected_platforms_keys' => $selected_platforms_keys,
					'default_user_avatar_url' => AUTHOR_KIT_PLUGIN_URL . 'admin/assets/files/default_user_avatar.jpg',

				)
			);
		}

		if ( 'post.php' === $hook || 'post-new.php' === $hook ) {
			wp_enqueue_script( 'author-kit-metabox-script', AUTHOR_KIT_PLUGIN_URL . 'admin/assets/js/author-kit-metabox.js', array( 'jquery' ), $this->version, true );
		}
	}


	/**
	 * Registers the admin menu pages and subpages for the Author Kit plugin.
	 *
	 * Dynamically loads the main menu icon from an SVG file, sets menu and page
	 * titles based on user capabilities, and adds the following menu items:
	 * - Main dashboard page accessible to users with 'list_users' capability.
	 * - Submenus for Posts, Author Profiles, Templates, Settings, User management,
	 *   and editing Author Profiles with appropriate capability checks.
	 *
	 * Handles capability-based visibility for menu items and defines callbacks
	 * for rendering each admin page.
	 *
	 * @return void
	 */
	public function admin_menu_pages() {

		$response = wp_remote_get( AUTHOR_KIT_PLUGIN_URL . 'admin/assets/files/author-kit-dashicon.svg' );
		if ( is_wp_error( $response ) ) {
			$icon_data = ''; // Handle error if request fails.
		} else {
			// Encoding SVG icon for inline image usage; not used for obfuscation.
			// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
			$icon_data = 'data:image/svg+xml;base64,' . base64_encode( wp_remote_retrieve_body( $response ) );
		}

		// Set dynamic menu and page titles based on user's capability.
		$header_page = current_user_can( 'create_users' ) ? 'author-kit-dashboard' : '';
		$page_title  = current_user_can( 'create_users' ) ? __( 'Add New User', 'author-kit' ) : __( 'Edit User', 'author-kit' );

		// Main menu page.
		add_menu_page(
			__( 'Author Kit', 'author-kit' ),         // Page title.
			__( 'Author Kit', 'author-kit' ),         // Menu title.
			'list_users',                           // Capability.
			'author-kit-dashboard',                 // Menu slug.
			array( $this, 'author_kit_dashboard_page' ),
			$icon_data,
			3                                       // Position (below Dashboard).
		);

		// Submenu pages.
		add_submenu_page(
			'author-kit-dashboard',                 // Parent slug.
			__( 'Author Kit Posts', 'author-kit' ),   // Page title.
			__( 'Posts', 'author-kit' ),              // Menu title.
			'edit_posts',                           // Capability.
			'author-kit-posts',                     // Menu slug.
			array( $this, 'author_kit_posts_page' )   // Callback function.
		);

		add_submenu_page(
			'author-kit-dashboard',
			__( 'Author Profiles', 'author-kit' ),
			__( 'Author Profiles', 'author-kit' ),
			'list_users',
			'author-kit-author-profiles',
			array( $this, 'author_kit_author_profiles_page' )
		);

		add_submenu_page(
			'author-kit-dashboard',
			__( 'Templates', 'author-kit' ),
			__( 'Templates', 'author-kit' ),
			'manage_options',
			'author-kit-templates',
			array( $this, 'author_kit_templates_page' )
		);

		add_submenu_page(
			'author-kit-dashboard',
			__( 'Settings', 'author-kit' ),
			__( 'Settings', 'author-kit' ),
			'manage_options',
			'author-kit-settings',
			array( $this, 'author_kit_settings_page' )
		);

		add_submenu_page(
			'',
			__( 'Add New User', 'author-kit' ),
			__( 'Add New User', 'author-kit' ),
			// Custom capability for Author Kit plugin.
			// phpcs:ignore WordPress.WP.Capabilities.Unknown
			'create_users',
			'author-kit-add-new-user',
			array( $this, 'author_kit_add_new_user_page' )
		);

		add_submenu_page(
			'',
			__( 'Edit User', 'author-kit' ),
			__( 'Edit User', 'author-kit' ),
			'edit_users',
			'author-kit-edit-user',
			array( $this, 'author_kit_edit_user_page' )
		);

		add_submenu_page(
			'',
			__( 'Edit Author Profile', 'author-kit' ),
			__( 'Edit Author Profile', 'author-kit' ),
			'read', // this will allow everyone to access this page except "Subscribers".
			'author-kit-update-author-profile',
			array( $this, 'author_kit_edit_author_profile_page' )
		);
	}

	/**
	 * Callback for the dashboard (user table) page
	 */
	public function author_kit_dashboard_page() {
		include AUTHOR_KIT_PLUGIN_PATH . 'admin/views/author-kit-dashboard.php';
	}

	/**
	 * Callback for the user add new page
	 */
	public function author_kit_add_new_user_page() {

		// verify nonce
		if (
		! isset( $_GET['add-new-user-nonce'] ) ||
		! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['add-new-user-nonce'] ) ), 'author-kit-add-new-user-nonce' )
		) {
			wp_die( esc_html__( 'Invalid nonce.', 'author-kit' ) );
		}

		// Verify Permission
		if ( ! current_user_can( 'create_users' ) ) {
			wp_die( esc_html__( 'Permission denied.', 'author-kit' ) );
		}

		$action_selected = 'add-new';
		include AUTHOR_KIT_PLUGIN_PATH . 'admin/views/author-kit-user.php';
	}

	/**
	 * Callback for the user edit user page
	 */
	public function author_kit_edit_user_page() {

		// Verify Nonce
		if (
		isset( $_GET['edit-user-nonce'] ) &&
		wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['edit-user-nonce'] ) ), 'author-kit-edit-user-nonce' ) &&
		isset( $_GET['user-id'] )
		) {
			$user_id = intval( $_GET['user-id'] );
		} else {
			wp_die( esc_html__( 'Invalid nonce.', 'author-kit' ) );
		}

		// Verify Permission
		if ( ! current_user_can( 'edit_users' ) ) {
			wp_die( esc_html__( 'Permission denied.', 'author-kit' ) );
		}

		// Verify User
		if ( $user_id ) {
			$user = get_userdata( $user_id );
			if ( ! $user ) {
				wp_die( esc_html__( 'User not found.', 'author-kit' ) );
			}
		} else {
			wp_die( esc_html__( 'User ID is missing for edit action.', 'author-kit' ) );
		}

		$action_selected = 'edit';
		include AUTHOR_KIT_PLUGIN_PATH . 'admin/views/author-kit-user.php';
	}

	/**
	 * Callback for the post table page
	 */
	public function author_kit_posts_page() {
		include AUTHOR_KIT_PLUGIN_PATH . 'admin/views/author-kit-posts-table.php';
	}

	/**
	 * Callback for the auhtor profile table page
	 */
	public function author_kit_author_profiles_page() {
		include AUTHOR_KIT_PLUGIN_PATH . 'admin/views/author-kit-profile-table.php';
	}

	/**
	 * Callback for the templates selection page
	 */
	public function author_kit_templates_page() {
		include AUTHOR_KIT_PLUGIN_PATH . 'admin/views/author-kit-templates.php';
	}

	/**
	 * Callback for the settings page
	 */
	public function author_kit_settings_page() {
		include AUTHOR_KIT_PLUGIN_PATH . 'admin/views/author-kit-settings.php';
	}

	/**
	 * Callback for the edit author profile page
	 */
	public function author_kit_edit_author_profile_page() {

		// Validate nonce
		if (
		isset( $_GET['update-author-profile-nonce'] ) &&
		wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['update-author-profile-nonce'] ) ), 'author-kit-update-author-profile-nonce' ) &&
		isset( $_GET['user_id'] )
		) {
			$user_id = intval( $_GET['user_id'] );
		} else {
			wp_die( esc_html__( 'Invalid or missing security token (nonce).', 'author-kit' ) );
		}

		// Permissions check
		if ( ! current_user_can( 'edit_users' ) && get_current_user_id() !== $user_id ) {
			wp_die( esc_html__( 'You are not allowed to edit this user profile.', 'author-kit' ) );
		}

		include AUTHOR_KIT_PLUGIN_PATH . 'admin/views/author-kit-update-author-profile.php';
	}
}
