<?php
/**
 * Template Name: Default
 *
 * PufferGo default header template (Tailwind + Alpine).
 * Flat nav links on desktop, no dropdowns. Uses context and shared mobile panel.
 *
 * @var PufferGo_Header_Data $data Header data (injected by render_variants).
 *
 * @package PufferGo
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<header class="puffergo-default-header bg-neutral-50 border-b border-neutral-200/10" x-data="{ mobileOpen: false }">
	<nav
		aria-label="<?php esc_attr_e( 'Global', 'puffergo' ); ?>"
		class="puffergo-header-nav puffergo-nav-align-wrapper <?php echo esc_attr( $data->nav_align_class ); ?> mx-auto flex max-w-7xl items-center justify-between gap-3 p-5 lg:px-8"
	>
		<a href="<?php echo esc_url( $data->home_url ); ?>" class="-m-1.5 flex shrink-0 items-center p-1.5 text-neutral-900 no-underline" aria-label="<?php echo esc_attr( $data->brand_name ); ?>">
		<?php if ( ! puffergo_the_logo( 'h-8 w-auto max-w-[12rem] object-contain object-left', array( 'fetchpriority' => 'high', 'loading' => 'eager' ) ) ) : ?>
			<span class="text-sm font-semibold"><?php echo esc_html( $data->brand_name ? $data->brand_name : __( 'Logo', 'puffergo' ) ); ?></span>
		<?php endif; ?>
		</a>

		<div class="puffergo-nav-spacer-left <?php echo esc_attr( $data->spacer_left_class ); ?>" aria-hidden="true"></div>

		<div class="flex lg:hidden">
			<button
				type="button"
				class="-m-2.5 inline-flex items-center justify-center rounded-md p-2.5 text-neutral-700"
				@click="mobileOpen = true"
				:aria-expanded="mobileOpen ? 'true' : 'false'"
				aria-controls="puffergo-mobile-menu-panel"
			>
				<span class="sr-only"><?php esc_html_e( 'Open main menu', 'puffergo' ); ?></span>
				<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" aria-hidden="true" class="size-6">
					<path d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" stroke-linecap="round" stroke-linejoin="round" />
				</svg>
			</button>
		</div>

		<div class="hidden lg:flex lg:items-center lg:gap-x-12">
			<?php foreach ( $data->menu_items as $pgo_item ) :
				$is_current = ($data->is_current)( $pgo_item );
				$link_class = 'text-sm font-semibold text-neutral-900 no-underline';
				if ( $is_current ) {
					$link_class .= ' text-brand-600';
				}
				?>
				<a href="<?php echo esc_url( $pgo_item->url ); ?>" class="<?php echo esc_attr( $link_class ); ?>">
					<?php echo esc_html( $pgo_item->title ); ?>
				</a>
			<?php endforeach; ?>
			<?php require PUFFERGO_PLUGIN_DIR . '/templates/components/header-empty-menu-notice.php'; ?>
		</div>

		<div class="puffergo-nav-spacer-right <?php echo esc_attr( $data->spacer_right_class ); ?>" aria-hidden="true"></div>
	</nav>

	<?php
	ob_start();
	?>
	<div class="flex flex-col gap-2 py-6">
		<?php foreach ( $data->menu_items as $pgo_item ) :
			$is_current   = ($data->is_current)( $pgo_item );
			$has_children = ! empty( $pgo_item->children ) && is_array( $pgo_item->children );
			$mobile_class = 'puffergo-nav-link block -mx-3 rounded-lg px-3 py-2 text-base font-semibold text-neutral-900 no-underline cursor-pointer';
			if ( $is_current ) {
				$mobile_class .= ' puffergo-nav-item-current text-brand-600';
			}

			if ( $has_children ) : ?>
				<div class="puffergo-nav-item-parent">
					<span class="block -mx-3 rounded-lg px-3 py-2 text-base font-semibold text-neutral-900"><?php echo esc_html( $pgo_item->title ); ?></span>
					<ul class="mt-1 list-none pl-4">
						<?php foreach ( $pgo_item->children as $pgo_child ) : ?>
							<li><a href="<?php echo esc_url( $pgo_child->url ); ?>" class="block -mx-3 rounded-lg px-3 py-2 text-sm text-neutral-600 no-underline cursor-pointer hover:bg-neutral-50 hover:text-neutral-900"><?php echo esc_html( $pgo_child->title ); ?></a></li>
						<?php endforeach; ?>
					</ul>
				</div>
			<?php else :
				printf(
					'<a href="%s" class="%s">%s</a>',
					esc_url( $pgo_item->url ),
					esc_attr( $mobile_class ),
					esc_html( $pgo_item->title )
				);
			endif;
		endforeach; ?>
	</div>
	<?php
	$pgo_header_mobile_panel_content = ob_get_clean();
	$pgo_header_mobile_open_var      = 'mobileOpen';
	$pgo_header_mobile_panel_title   = __( 'Menu', 'puffergo' );
	require PUFFERGO_PLUGIN_DIR . '/templates/components/mobile-panel.php';
	?>
</header>
