<?php
/**
 * Admin Menu
 *
 * Handles RedFox WP admin menu creation and management.
 *
 * @package RedFoxWP\RedFoxTabManager
 * @since 1.0.0
 */

namespace RedFoxWP\RedFoxTabManager;

use RedFoxWP\RedFoxTabManager\Abstracts\Abstract_Singleton;

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

/**
 * Admin Menu class
 *
 * Manages the main RedFox WP admin menu structure.
 * Individual plugins can register their own submenus.
 *
 * @since 1.0.0
 */
class Admin_Menu extends Abstract_Singleton {

	/**
	 * Constructor
	 *
	 * @since 1.0.0
	 * @access protected
	 *
	 * @return void
	 */
	protected function __construct() {
		$this->hooks();
	}

	/**
	 * Initialize WordPress hooks
	 *
	 * @since 1.0.0
	 * @access private
	 *
	 * @return void
	 */
	private function hooks() {
		add_action( 'admin_menu', array( $this, 'add_admin_menus' ) );
		add_action( 'admin_menu', array( $this, 'remove_duplicate_submenu' ), 999 );
		add_filter( 'plugin_action_links_' . plugin_basename( REDFOX_TAB_MANAGER_PLUGIN_FILE ), array( $this, 'add_plugin_action_links' ) );
	}

	/**
	 * Add main RedFox WP admin menu and Tab Manager submenus
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return void
	 */
	public function add_admin_menus() {
		// Create or get existing RedFox WP main menu.
		$main_menu_slug = 'redfox-wp';
		$menu_exists    = $this->menu_exists( $main_menu_slug );

		if ( ! $menu_exists ) {
			// Create main RedFox WP menu (no callback - will redirect to first submenu).
			add_menu_page(
				__( 'RedFox WP', 'redfox-tab-manager' ),
				__( 'RedFox WP', 'redfox-tab-manager' ),
				'manage_options',
				$main_menu_slug,
				'__return_false', // No callback - will redirect to first submenu.
				$this->get_redfox_menu_icon(),
				25
			);
		}

		// Add Tab Manager submenu.
		add_submenu_page(
			$main_menu_slug,
			__( 'Tab Manager', 'redfox-tab-manager' ),
			__( 'Tab Manager', 'redfox-tab-manager' ),
			'manage_options',
			'redfox-tab-manager',
			array( $this, 'render_tab_manager_page' )
		);
	}

	/**
	 * Remove duplicate submenu that WordPress creates automatically
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return void
	 */
	public function remove_duplicate_submenu() {
		$main_menu_slug = 'redfox-wp';

		// Remove the automatic submenu that WordPress creates with the same name as the main menu.
		// This must be done after all submenu pages have been added (priority 999).
		remove_submenu_page( $main_menu_slug, $main_menu_slug );
	}

	/**
	 * Render the Tab Manager admin page
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return void
	 */
	public function render_tab_manager_page() {
		// Check user capabilities.
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'redfox-tab-manager' ) );
		}
		?>
		<div class="wrap">
			<div id="redfox-tab-manager-root"></div>
		</div>
		<?php
	}

	/**
	 * Get the custom RedFox menu icon as base64 encoded SVG data URI
	 *
	 * @since 1.0.2
	 * @access private
	 *
	 * @return string Base64 encoded SVG data URI for the menu icon.
	 */
	private function get_redfox_menu_icon() {
		return 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNjMiIGhlaWdodD0iNzAiIHZpZXdCb3g9IjAgMCA2MyA3MCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik0xMC42ODUzIDEwLjEzODFDMTEuMTA1OSA5LjkyMTkzIDExLjYxMTEgOS45NjE4OSAxMS45OTMxIDEwLjI0MTVMMjUuMDA3OSAxOS43Njg0SDM3LjEwMzdMNTEuMDM4OCAxMC4yMTg5QzUxLjQyMzQgOS45NTUzNyA1MS45MjEgOS45Mjc3NCA1Mi4zMzIgMTAuMTQ3MUM1Mi43NDMgMTAuMzY2NSA1MyAxMC43OTcgNTMgMTEuMjY1OFYzMi43ODY4QzUzIDMzLjA1NTMgNTIuOTE1MyAzMy4zMTY5IDUyLjc1ODIgMzMuN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cuNDA0NyAzMi4yMjE4TDM5Ljk4NTQgMzQuMjE1MUw0OC45NjcxIDMyLjEzMjFMMzguNDYwOCAyMy40Njk4WiIgLz4KPC9zdmc+Cg==';
	}

	/**
	 * Add plugin action links to the plugins page
	 *
	 * Adds a "Tab Manager" link to the plugin's action links on the plugins page.
	 *
	 * @since 1.0.2
	 * @access public
	 *
	 * @param array $links Existing plugin action links.
	 *
	 * @return array Modified plugin action links.
	 */
	public function add_plugin_action_links( $links ) {
		$settings_link = sprintf(
			'<a href="%s">%s</a>',
			esc_url( admin_url( 'admin.php?page=redfox-tab-manager' ) ),
			esc_html__( 'Tab Manager', 'redfox-tab-manager' )
		);

		// Add the settings link at the beginning of the array.
		array_unshift( $links, $settings_link );

		return $links;
	}

	/**
	 * Check if admin menu exists
	 *
	 * @since 1.0.0
	 * @access private
	 *
	 * @param string $menu_slug Menu slug to check.
	 *
	 * @return bool True if menu exists, false otherwise.
	 */
	private function menu_exists( $menu_slug ) {
		global $menu;

		// Use nullish coalescing operator as per project requirements.
		$menu_items = $menu ?? array();

		if ( ! is_array( $menu_items ) ) {
			return false;
		}

		foreach ( $menu_items as $menu_item ) {
			$menu_item = $menu_item ?? array();
			if ( isset( $menu_item[2] ) && $menu_item[2] === $menu_slug ) {
				return true;
			}
		}

		return false;
	}
}
