<?php
/**
 * Persistent admin header — view partial.
 *
 * Expects in scope:
 *   - $nav           array  Primary nav items (slug, label, icon, pro flag).
 *   - $subtabs       array  Settings sub-tabs.
 *   - $status        array  Live status (level, label, detail, fps, expires).
 *   - $notifications array  Notification cards to list.
 *   - $user          array  { name, email, initial }.
 *   - $is_pro        bool   License tier.
 *   - $current       string Current page slug.
 *   - $current_tab   string Current Settings sub-tab.
 *   - $docs_url      string Contextual documentation URL for the current page.
 *
 * @package TrustLens\Admin\Views
 * @since   1.2.2
 */

defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- View template; variables are scoped to the including method, not the global namespace.

$status_pill_class = 'wstl-hdr-status wstl-hdr-status-' . sanitize_html_class( $status['level'] );
?>

<!-- Icon sprite (inline, scoped with ids prefixed tl- to avoid collisions) -->
<svg width="0" height="0" class="wstl-hdr-sprite" aria-hidden="true"><defs>
<symbol id="tl-i-shield" viewBox="0 0 24 24"><path d="M12 2L4 6v6c0 5 3.5 9.5 8 10 4.5-.5 8-5 8-10V6l-8-4z"/></symbol>
<symbol id="tl-i-home" viewBox="0 0 24 24"><path d="M3 12l9-9 9 9"/><path d="M5 10v10h14V10"/></symbol>
<symbol id="tl-i-users" viewBox="0 0 24 24"><circle cx="9" cy="7" r="4"/><path d="M2 21v-2a5 5 0 015-5h4a5 5 0 015 5v2"/><circle cx="17" cy="7" r="3"/><path d="M22 21v-2a4 4 0 00-3-3.87"/></symbol>
<symbol id="tl-i-bell" viewBox="0 0 24 24"><path d="M18 8a6 6 0 10-12 0c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.7 21a2 2 0 01-3.4 0"/></symbol>
<symbol id="tl-i-zap" viewBox="0 0 24 24"><path d="M13 2L3 14h9l-1 8 10-12h-9l1-8z"/></symbol>
<symbol id="tl-i-chart" viewBox="0 0 24 24"><path d="M3 3v18h18"/><path d="M7 14l4-4 4 4 5-5"/></symbol>
<symbol id="tl-i-settings" viewBox="0 0 24 24"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 00.3 1.8l.1.1a2 2 0 11-2.8 2.8l-.1-.1a1.7 1.7 0 00-1.8-.3 1.7 1.7 0 00-1 1.5V21a2 2 0 01-4 0v-.1a1.7 1.7 0 00-1-1.5 1.7 1.7 0 00-1.8.3l-.1.1a2 2 0 11-2.8-2.8l.1-.1a1.7 1.7 0 00.3-1.8 1.7 1.7 0 00-1.5-1H3a2 2 0 010-4h.1a1.7 1.7 0 001.5-1 1.7 1.7 0 00-.3-1.8l-.1-.1a2 2 0 112.8-2.8l.1.1a1.7 1.7 0 001.8.3H9a1.7 1.7 0 001-1.5V3a2 2 0 014 0v.1a1.7 1.7 0 001 1.5 1.7 1.7 0 001.8-.3l.1-.1a2 2 0 112.8 2.8l-.1.1a1.7 1.7 0 00-.3 1.8V9a1.7 1.7 0 001.5 1H21a2 2 0 010 4h-.1a1.7 1.7 0 00-1.5 1z"/></symbol>
<symbol id="tl-i-search" viewBox="0 0 24 24"><circle cx="11" cy="11" r="8"/><path d="M21 21l-4.3-4.3"/></symbol>
<symbol id="tl-i-help" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10"/><path d="M9.09 9a3 3 0 015.83 1c0 2-3 3-3 3"/><path d="M12 17h0"/></symbol>
<symbol id="tl-i-menu" viewBox="0 0 24 24"><path d="M3 6h18M3 12h18M3 18h18"/></symbol>
<symbol id="tl-i-logout" viewBox="0 0 24 24"><path d="M9 21H5a2 2 0 01-2-2V5a2 2 0 012-2h4"/><path d="M16 17l5-5-5-5"/><path d="M21 12H9"/></symbol>
<symbol id="tl-i-book" viewBox="0 0 24 24"><path d="M4 19.5A2.5 2.5 0 016.5 17H20"/><path d="M6.5 2H20v20H6.5A2.5 2.5 0 014 19.5v-15A2.5 2.5 0 016.5 2z"/></symbol>
<symbol id="tl-i-key" viewBox="0 0 24 24"><path d="M21 2l-2 2m-7.61 7.61a5.5 5.5 0 11-7.778 7.778 5.5 5.5 0 017.777-7.777zm0 0L15.5 7.5m0 0l3 3L22 7l-3-3m-3.5 3.5L19 4"/></symbol>
<symbol id="tl-i-arrow-r" viewBox="0 0 24 24"><path d="M5 12h14M13 6l6 6-6 6"/></symbol>
<symbol id="tl-i-csv" viewBox="0 0 24 24"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><path d="M14 2v6h6"/></symbol>
<symbol id="tl-i-check" viewBox="0 0 24 24"><path d="M20 6L9 17l-5-5"/></symbol>
</defs></svg>

<header class="wstl-hdr" id="wstl-hdr">

	<!-- Row 1 — identity / search / status / actions -->
	<div class="wstl-hdr-top">
		<a class="wstl-hdr-logo" href="<?php echo esc_url( admin_url( 'admin.php?page=trustlens' ) ); ?>">
			<span class="wstl-hdr-mark">
				<img src="<?php echo esc_url( TRUSTLENS_PLUGIN_URL . 'assets/images/icon.svg' ); ?>"
					alt="<?php esc_attr_e( 'TrustLens', 'trustlens' ); ?>"
					width="34" height="34" />
			</span>
			<span>TrustLens<small><?php echo $is_pro ? esc_html__( 'Pro · Admin', 'trustlens' ) : esc_html__( 'Admin', 'trustlens' ); ?></small></span>
		</a>

		<button class="wstl-hdr-burger" id="wstl-hdr-burger"
			aria-label="<?php esc_attr_e( 'Open menu', 'trustlens' ); ?>"
			aria-controls="wstl-hdr-navwrap" aria-expanded="false">
			<svg class="wstl-hdr-icon"><use href="#tl-i-menu"/></svg>
		</button>

		<button class="wstl-hdr-search" id="wstl-hdr-search" aria-label="<?php esc_attr_e( 'Open search', 'trustlens' ); ?>">
			<svg class="wstl-hdr-icon"><use href="#tl-i-search"/></svg>
			<span class="placeholder"><?php esc_html_e( 'Search features, settings, actions…', 'trustlens' ); ?></span>
			<span class="wstl-hdr-kbd">⌘K</span>
		</button>

		<button class="wstl-hdr-ib wstl-hdr-search-mobile" id="wstl-hdr-search-mobile" aria-label="<?php esc_attr_e( 'Open search', 'trustlens' ); ?>">
			<svg class="wstl-hdr-icon"><use href="#tl-i-search"/></svg>
		</button>

		<div class="wstl-hdr-right">

			<!-- Unsaved-changes indicator (revealed by JS when a settings form is dirty) -->
			<span class="wstl-hdr-unsaved" id="wstl-hdr-unsaved" role="status" hidden>
				<span class="wstl-hdr-unsaved-dot" aria-hidden="true"></span>
				<?php esc_html_e( 'Unsaved changes', 'trustlens' ); ?>
			</span>

			<!-- Live status pill -->
			<div class="wstl-hdr-rel">
				<button class="<?php echo esc_attr( $status_pill_class ); ?>" id="wstl-hdr-statusbtn" aria-haspopup="true" aria-expanded="false"
					aria-label="<?php
						/* translators: %s: live status label e.g. All clear, Panic Freeze */
						echo esc_attr( sprintf( __( 'Live status: %s', 'trustlens' ), $status['label'] ) );
					?>"
					title="<?php echo esc_attr( $status['label'] ); ?>">
					<span class="dot"></span>
					<span><?php echo esc_html( $status['label'] ); ?></span>
				</button>
				<div class="wstl-hdr-pop" id="wstl-hdr-statuspop" role="dialog">
					<div class="wstl-hdr-pop-section">
						<div class="wstl-hdr-pop-head">
							<?php esc_html_e( 'Current status', 'trustlens' ); ?>
						</div>
						<div class="wstl-hdr-pop-title"><?php echo esc_html( $status['label'] ); ?></div>
						<?php if ( ! empty( $status['detail'] ) ) : ?>
							<div class="wstl-hdr-pop-detail"><?php echo esc_html( $status['detail'] ); ?></div>
						<?php endif; ?>
						<?php if ( ! empty( $status['expires'] ) ) : ?>
							<div class="wstl-hdr-pop-detail">
								<?php
								/* translators: %s: formatted expiry time. */
								printf( esc_html__( 'Expires at %s', 'trustlens' ), esc_html( date_i18n( 'H:i', (int) $status['expires'] ) ) );
								?>
							</div>
						<?php endif; ?>

						<?php if ( ! empty( $status['fps'] ) ) : ?>
							<ul class="wstl-hdr-pop-fps">
								<?php foreach ( $status['fps'] as $fp ) : ?>
									<li>
										<span class="hash"><?php echo esc_html( $fp['hash'] ); ?></span>
										<span class="ttl"><?php echo esc_html( $fp['ttl'] ); ?></span>
									</li>
								<?php endforeach; ?>
							</ul>
						<?php endif; ?>

						<a class="wstl-hdr-pop-cta" href="<?php echo esc_url( admin_url( 'admin.php?page=trustlens-card-testing' ) ); ?>">
							<?php esc_html_e( 'Open Card-Testing Defense', 'trustlens' ); ?>
						</a>
					</div>
				</div>
			</div>

			<span class="wstl-hdr-tier wstl-hdr-tier-<?php echo $is_pro ? 'pro' : 'free'; ?>">
				<?php echo $is_pro ? esc_html__( 'Pro', 'trustlens' ) : esc_html__( 'Free', 'trustlens' ); ?>
			</span>

			<!-- Notifications -->
			<div class="wstl-hdr-rel">
				<button class="wstl-hdr-ib" id="wstl-hdr-bellbtn" aria-label="<?php esc_attr_e( 'Notifications', 'trustlens' ); ?>" aria-haspopup="true" aria-expanded="false">
					<svg class="wstl-hdr-icon"><use href="#tl-i-bell"/></svg>
					<?php if ( ! empty( $notifications ) ) : ?>
						<span class="wstl-hdr-badge"><?php echo esc_html( (string) count( $notifications ) ); ?></span>
					<?php endif; ?>
				</button>
				<div class="wstl-hdr-pop wstl-hdr-pop-wide" id="wstl-hdr-bellpop" role="dialog">
					<div class="wstl-hdr-pop-section">
						<div class="wstl-hdr-pop-head">
							<?php esc_html_e( 'Notifications', 'trustlens' ); ?>
						</div>
						<?php if ( empty( $notifications ) ) : ?>
							<div class="wstl-hdr-pop-empty"><?php esc_html_e( 'You\'re all caught up.', 'trustlens' ); ?></div>
						<?php else : ?>
							<?php foreach ( $notifications as $n ) : ?>
								<a class="wstl-hdr-notif wstl-hdr-notif-<?php echo esc_attr( $n['level'] ); ?>" href="<?php echo esc_url( $n['href'] ); ?>">
									<span class="wstl-hdr-notif-icon"><svg class="wstl-hdr-icon"><use href="#tl-<?php echo esc_attr( $n['icon'] ); ?>"/></svg></span>
									<span class="wstl-hdr-notif-body">
										<span class="t"><?php echo esc_html( $n['title'] ); ?></span>
										<span class="d"><?php echo esc_html( $n['detail'] ); ?></span>
									</span>
									<svg class="wstl-hdr-icon wstl-hdr-notif-arrow" aria-hidden="true"><use href="#tl-i-arrow-r"/></svg>
								</a>
							<?php endforeach; ?>
							<p class="wstl-hdr-pop-note">
								<?php esc_html_e( 'Notifications reflect live plugin state — they clear automatically when the condition resolves.', 'trustlens' ); ?>
							</p>
						<?php endif; ?>
					</div>
					<div class="wstl-hdr-pop-section wstl-hdr-pop-foot">
						<a class="wstl-hdr-menu-item" href="<?php echo esc_url( admin_url( 'admin.php?page=trustlens-notifications' ) ); ?>">
							<svg class="wstl-hdr-icon"><use href="#tl-i-bell"/></svg>
							<span><?php esc_html_e( 'Open Notifications settings', 'trustlens' ); ?></span>
							<svg class="wstl-hdr-icon wstl-hdr-menu-arrow"><use href="#tl-i-arrow-r"/></svg>
						</a>
					</div>
				</div>
			</div>

			<a class="wstl-hdr-ib" href="<?php echo esc_url( $docs_url ); ?>" target="_blank" rel="noopener"
				title="<?php esc_attr_e( 'Help & docs', 'trustlens' ); ?>"
				aria-label="<?php esc_attr_e( 'Help & docs', 'trustlens' ); ?>">
				<svg class="wstl-hdr-icon"><use href="#tl-i-help"/></svg>
			</a>

			<!-- Avatar / user menu -->
			<div class="wstl-hdr-rel">
				<button class="wstl-hdr-avatar" id="wstl-hdr-avatarbtn" aria-label="<?php esc_attr_e( 'Account menu', 'trustlens' ); ?>" aria-haspopup="true" aria-expanded="false">
					<?php echo esc_html( $user['initial'] ); ?>
				</button>
				<div class="wstl-hdr-pop" id="wstl-hdr-avatarpop" role="dialog">
					<div class="wstl-hdr-pop-ident">
						<span class="wstl-hdr-avatar"><?php echo esc_html( $user['initial'] ); ?></span>
						<div>
							<div class="n"><?php echo esc_html( $user['name'] ); ?></div>
							<?php if ( ! empty( $user['email'] ) ) : ?>
								<div class="e"><?php echo esc_html( $user['email'] ); ?></div>
							<?php endif; ?>
						</div>
					</div>
					<div class="wstl-hdr-pop-menu">
						<a class="wstl-hdr-menu-item" href="<?php echo esc_url( function_exists( 'wstl_get_upgrade_or_fallback_url' ) ? wstl_get_upgrade_or_fallback_url() : admin_url() ); ?>">
							<svg class="wstl-hdr-icon"><use href="#tl-i-key"/></svg>
							<span><?php echo $is_pro ? esc_html__( 'Manage license', 'trustlens' ) : esc_html__( 'Upgrade to Pro', 'trustlens' ); ?></span>
							<?php if ( ! $is_pro ) : ?>
								<span class="wstl-hdr-tag-pro"><?php esc_html_e( 'PRO', 'trustlens' ); ?></span>
							<?php endif; ?>
						</a>
						<a class="wstl-hdr-menu-item" href="<?php echo esc_url( $docs_url ); ?>" target="_blank" rel="noopener">
							<svg class="wstl-hdr-icon"><use href="#tl-i-book"/></svg>
							<span><?php esc_html_e( 'Documentation', 'trustlens' ); ?></span>
						</a>
						<a class="wstl-hdr-menu-item" href="<?php echo esc_url( admin_url( 'admin.php?page=trustlens-settings' ) ); ?>">
							<svg class="wstl-hdr-icon"><use href="#tl-i-settings"/></svg>
							<span><?php esc_html_e( 'Settings', 'trustlens' ); ?></span>
						</a>
					</div>
				</div>
			</div>

		</div>
	</div>

	<!-- Row 2 — primary nav -->
	<div class="wstl-hdr-nav-wrap" id="wstl-hdr-navwrap">
		<nav class="wstl-hdr-nav" role="navigation">
			<?php foreach ( $nav as $item ) :
				$url = admin_url( 'admin.php?page=' . $item['slug'] );
				$is_current = ( $current === $item['slug'] );
				$classes = 'wstl-hdr-tab';
				if ( $is_current ) { $classes .= ' on'; }
				?>
				<a class="<?php echo esc_attr( $classes ); ?>" href="<?php echo esc_url( $url ); ?>">
					<svg class="wstl-hdr-icon"><use href="#tl-<?php echo esc_attr( $item['icon'] ); ?>"/></svg>
					<span><?php echo esc_html( $item['label'] ); ?></span>
					<?php if ( ! empty( $item['pro'] ) ) : ?>
						<span class="wstl-hdr-tag-pro"><?php esc_html_e( 'PRO', 'trustlens' ); ?></span>
					<?php endif; ?>
				</a>
			<?php endforeach; ?>
		</nav>
	</div>

	<!-- Row 3 — Settings sub-tabs (rendered only on Settings page) -->
	<?php if ( 'trustlens-settings' === $current ) : ?>
		<div class="wstl-hdr-subnav">
			<div class="wstl-hdr-subnav-inner">
				<?php foreach ( $subtabs as $slug => $meta ) :
					$url = admin_url( 'admin.php?page=trustlens-settings&tab=' . $slug );
					$classes = 'wstl-hdr-subtab';
					if ( $current_tab === $slug ) { $classes .= ' on'; }
					?>
					<a class="<?php echo esc_attr( $classes ); ?>" href="<?php echo esc_url( $url ); ?>">
						<?php echo esc_html( $meta['label'] ); ?>
						<?php if ( ! empty( $meta['pro'] ) ) : ?>
							<span class="wstl-hdr-tag-pro"><?php esc_html_e( 'PRO', 'trustlens' ); ?></span>
						<?php endif; ?>
					</a>
				<?php endforeach; ?>
			</div>
		</div>
	<?php endif; ?>
</header>

<!-- Command palette (hidden until ⌘K / search button) -->
<div class="wstl-cmdp-overlay" id="wstl-cmdp-overlay"></div>
<div class="wstl-cmdp" id="wstl-cmdp" role="dialog" aria-label="<?php esc_attr_e( 'Command palette', 'trustlens' ); ?>">
	<div class="wstl-cmdp-input-wrap">
		<svg class="wstl-hdr-icon"><use href="#tl-i-search"/></svg>
		<input class="wstl-cmdp-input" id="wstl-cmdp-input" placeholder="<?php esc_attr_e( 'Search a feature, setting, or action…', 'trustlens' ); ?>" autocomplete="off">
		<span class="wstl-cmdp-esc">ESC</span>
	</div>
	<div class="wstl-cmdp-results" id="wstl-cmdp-results"></div>
	<div class="wstl-cmdp-foot">
		<span><kbd>↑</kbd><kbd>↓</kbd> <?php esc_html_e( 'navigate', 'trustlens' ); ?></span>
		<span><kbd>↵</kbd> <?php esc_html_e( 'select', 'trustlens' ); ?></span>
		<span><kbd>esc</kbd> <?php esc_html_e( 'close', 'trustlens' ); ?></span>
	</div>
</div>
