<?php
/**
 * Menu Items Class
 *
 * This class adds menu items for the plugin in the WordPress dashboard.
 * It handles the creation of the main menu and sub-menus.
 *
 * @package miniorange-sms-order-notification-otp-verification/helper
 * @since   1.0.0
 */

namespace WCSMSOTP\Helper;

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

use WCSMSOTP\MoWcInit;
use WCSMSOTP\Objects\PluginPageDetails;
use WCSMSOTP\Objects\TabDetails;
use WCSMSOTP\Traits\Instance;

/**
 * Menu Items Class
 *
 * This class adds menu items for the plugin in the WordPress dashboard.
 * It handles the creation of the main menu and sub-menus.
 */
if ( ! class_exists( 'MenuItems' ) ) {
	/**
	 * Menu Items Class
	 *
	 * This class adds menu items for the plugin in the WordPress dashboard.
	 * It handles the creation of the main menu and sub-menus.
	 */
	final class MenuItems {

		use Instance;

		/**
		 * The callback function for the menu items.
		 *
		 * @var callable
		 */
		private $callback;

		/**
		 * The slug for the main menu.
		 *
		 * @var string
		 */
		private $menu_slug;

		/**
		 * The URL for the plugin icon to be shown in the dashboard.
		 *
		 * @var string
		 */
		private $menu_logo;

		/**
		 * Array of PluginPageDetails objects detailing all the page menu options.
		 *
		 * @var array<PluginPageDetails>
		 */
		private $tab_details;

		/**
		 * Constructor.
		 *
		 * Initializes menu items and adds them to the WordPress dashboard.
		 */
		private function __construct() {
			$this->callback  = array( MoWcInit::instance(), 'mo_wc_customer_validation_options' );
			$this->menu_logo = defined( 'MOV_WC_ICON' ) ? esc_url( MOV_WC_ICON ) : '';

			if ( ! class_exists( 'WCSMSOTP\Objects\TabDetails' ) ) {
				return;
			}
			$tab_details = TabDetails::instance();
			if ( ! is_object( $tab_details ) ) {
				return;
			}
			$this->tab_details = isset( $tab_details->tab_details ) && is_array( $tab_details->tab_details ) ? $tab_details->tab_details : array();
			$this->menu_slug   = isset( $tab_details->parent_slug ) && is_string( $tab_details->parent_slug ) ? sanitize_key( $tab_details->parent_slug ) : '';
			if ( ! empty( $this->menu_slug ) ) {
				$this->add_main_menu();
				$this->add_sub_menus();
			}
		}
		/**
		 * Add main menu.
		 *
		 * Adds the main menu page to the WordPress dashboard.
		 *
		 * @return void
		 */
		private function add_main_menu() {
			if ( empty( $this->menu_slug ) || ! is_callable( $this->callback ) ) {
				return;
			}
			$page_title = esc_html__( 'WC Notifications', 'miniorange-sms-order-notification-otp-verification' );
			$menu_title = esc_html__( 'WC Notifications', 'miniorange-sms-order-notification-otp-verification' );
			$menu_slug  = sanitize_key( $this->menu_slug );
			$menu_logo  = ! empty( $this->menu_logo ) ? esc_url( $this->menu_logo ) : '';

			add_menu_page(
				$page_title,
				$menu_title,
				'manage_options',
				$menu_slug,
				$this->callback,
				$menu_logo
			);
		}

		/**
		 * Add sub-menus.
		 *
		 * Adds sub-menu pages to the WordPress dashboard.
		 *
		 * @return void
		 */
		private function add_sub_menus() {
			if ( ! is_array( $this->tab_details ) || empty( $this->tab_details ) ) {
				return;
			}
			if ( empty( $this->menu_slug ) || ! is_callable( $this->callback ) ) {
				return;
			}
			$parent_slug = sanitize_key( $this->menu_slug );

			foreach ( $this->tab_details as $tab_detail ) {
				if ( ! is_object( $tab_detail ) ) {
					continue;
				}
				$page_title = isset( $tab_detail->page_title ) && is_string( $tab_detail->page_title ) ? esc_html( $tab_detail->page_title ) : '';
				$menu_title = isset( $tab_detail->menu_title ) && is_string( $tab_detail->menu_title ) ? esc_html( $tab_detail->menu_title ) : '';
				$menu_slug  = isset( $tab_detail->menu_slug ) && is_string( $tab_detail->menu_slug ) ? sanitize_key( $tab_detail->menu_slug ) : '';

				if ( empty( $page_title ) || empty( $menu_title ) || empty( $menu_slug ) ) {
					continue;
				}

				add_submenu_page(
					$parent_slug,
					$page_title,
					$menu_title,
					'manage_options',
					$menu_slug,
					$this->callback
				);
			}
		}
	}
}
