<?php
/**
 * Shared horizontal admin navigation for FoodOrd (theme + core + pro).
 *
 * @package FoodOrd
 */

defined('ABSPATH') || exit;

if (!function_exists('foodord_admin_render_horizontal_menu')) {
	/**
	 * Render a shared horizontal menu for all FoodOrd admin pages.
	 *
	 * @param array{
	 *  	parent_slug?: string,
	 *  	exclude?: string[]
	 * } $args Optional arguments.
	 */
	function foodord_admin_render_horizontal_menu(array $args = array()): void
	{
		if (!is_admin() || !current_user_can('manage_options')) {
			return;
		}

		$parent_slug = '';
		if (isset($args['parent_slug']) && is_string($args['parent_slug'])) {
			$parent_slug = sanitize_key($args['parent_slug']);
		}
		if ('' === $parent_slug) {
			$parent_slug = function_exists('foodord_get_admin_parent_slug')
				? (string) foodord_get_admin_parent_slug()
				: 'foodord-theme-settings';
		}

		$exclude = array();
		if (isset($args['exclude']) && is_array($args['exclude'])) {
			$exclude = array_values(array_filter(array_map('sanitize_key', $args['exclude'])));
		}

		global $submenu, $pagenow;
		if ( ! is_array( $submenu ) || ! isset( $submenu[ $parent_slug ] ) || ! is_array( $submenu[ $parent_slug ] ) ) {
			return;
		}

		$current_page_raw = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
		$current_page     = is_string( $current_page_raw ) ? sanitize_key( $current_page_raw ) : '';
		$current_post_type = filter_input( INPUT_GET, 'post_type', FILTER_SANITIZE_FULL_SPECIAL_CHARS );

		$items = array();
		foreach ( $submenu[ $parent_slug ] as $entry ) {
			if ( ! is_array( $entry ) || ! isset( $entry[0], $entry[1], $entry[2] ) ) {
				continue;
			}

			$menu_title = (string) $entry[0];
			$capability = (string) $entry[1];
			$slug       = (string) $entry[2];
			$slug_key   = sanitize_key( $slug );

			if ( '' === $slug_key || $parent_slug === $slug_key ) {
				continue;
			}
			if ( in_array( $slug_key, $exclude, true ) ) {
				continue;
			}
			if ( '' !== $capability && ! current_user_can( $capability ) ) {
				continue;
			}

			if ( 0 === strpos( $slug, 'edit.php' ) ) {
				// Handle native list tables like Timed Products (edit.php?post_type=foodord_timed)
				$url = admin_url( $slug );
			} else {
				$url = function_exists( 'menu_page_url' ) ? menu_page_url( $slug, false ) : '';
				if ( ! is_string( $url ) || '' === $url ) {
					$url = admin_url( 'admin.php?page=' . rawurlencode( $slug ) );
				}
			}

			// Determine active state
			$is_active = false;
			
			// 1. Check standard page slug
			if ( '' !== $current_page && $slug_key === $current_page ) {
				$is_active = true;
			}
			// 2. Check post type pages (e.g. edit.php?post_type=foodord_timed)
			elseif ( $current_post_type && false !== strpos( $slug, 'post_type=' . $current_post_type ) ) {
				// Verify we are on the correct base file (edit.php)
				if ( isset( $pagenow ) && false !== strpos( $slug, $pagenow ) ) {
					$is_active = true;
				}
			}

			$items[] = array(
				'label'  => $menu_title,
				'url'    => $url,
				'active' => $is_active,
			);
		}

		$items = apply_filters('foodord_admin_horizontal_menu_items', $items, $parent_slug, $current_page);
		if (!is_array($items) || empty($items)) {
			return;
		}

		$allowed_label_html = array(
			'span' => array(
				'class' => true,
				'aria-hidden' => true,
			),
		);
		$toggle_more = esc_html__('More', 'foodord');
		$toggle_less = esc_html__('Less', 'foodord');
		?>
		<nav class="foodord-admin-nav" aria-label="<?php echo esc_attr__('FoodOrd navigation', 'foodord'); ?>">
			<div class="foodord-admin-nav__wrapper">
				<ul class="foodord-admin-nav__list">
					<?php foreach ($items as $item): ?>
						<?php
						if (!is_array($item)) {
							continue;
						}
						$label = isset($item['label']) ? (string) $item['label'] : '';
						$url = isset($item['url']) ? (string) $item['url'] : '';
						$active = !empty($item['active']);
						if ('' === $label || '' === $url) {
							continue;
						}
						$link_classes = array('foodord-admin-nav__link');
						if ($active) {
							$link_classes[] = 'is-active';
						}
						?>
						<li class="foodord-admin-nav__item">
							<a class="<?php echo esc_attr(implode(' ', $link_classes)); ?>"
								href="<?php echo esc_url($url); ?>" <?php echo $active ? 'aria-current="page"' : ''; ?>>
								<?php echo wp_kses($label, $allowed_label_html); ?>
							</a>
						</li>
					<?php endforeach; ?>
				</ul>

				<div class="foodord-admin-nav__toggle-wrap" hidden>
					<button type="button" class="foodord-admin-nav__toggle" aria-expanded="false"
						data-more-text="<?php echo esc_attr($toggle_more); ?>"
						data-less-text="<?php echo esc_attr($toggle_less); ?>">
						<span class="dashicons dashicons-menu" aria-hidden="true"></span>
						<span class="foodord-admin-nav__toggle-text"><?php echo esc_html($toggle_more); ?></span>
					</button>
					<ul class="foodord-admin-nav__dropdown">
						<!-- Wrapped items moved here via JS -->
					</ul>
				</div>
			</div>
		</nav>
		<?php
	}
}
