<?php
defined( 'ABSPATH' ) || exit;

$current_lang      = function_exists( 'avi_get_current_lang' ) ? avi_get_current_lang() : 'en';
$current_uri  = isset( $_SERVER['REQUEST_URI'] ) ? strtolower( (string) wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
$current_path = trim( (string) parse_url( $current_uri, PHP_URL_PATH ), '/' );

if ( ! function_exists( 'avi_switch_lang_url' ) ) {
	function avi_switch_lang_url( string $code ): string {
		$code = in_array( $code, array( 'en', 'fr', 'es' ), true ) ? $code : 'en';
		$full = isset( $_SERVER['REQUEST_URI'] ) ? strtok( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), '?' ) : '/';
		$slug_map = array(
			'/roadmap/'              => array( 'fr' => '/fr/feuille-de-route/',  'es' => '/es/hoja-de-ruta/' ),
			'/local-business/'       => array( 'fr' => '/fr/visibilite-locale/', 'es' => '/es/visibilidad-local/' ),
			'/pricing/'              => array( 'fr' => '/fr/tarifs/',            'es' => '/es/precios/' ),
			'/agency-pricing/'       => array( 'fr' => '/fr/tarifs-agences/',    'es' => '/es/precios-agencias/' ),
			'/how-it-works/'         => array( 'fr' => '/fr/comment-ca-marche/', 'es' => '/es/como-funciona/' ),
			'/about/'                => array( 'fr' => '/fr/a-propos/',          'es' => '/es/sobre-nosotros/' ),
			'/nonprofits/'           => array( 'fr' => '/fr/associations/',      'es' => '/es/organizaciones/' ),
			'/ambassador-program/'         => array( 'fr' => '/fr/programme-ambassadrice/', 'es' => '/es/programa-embajadora/' ),
			'/contact/'              => array( 'fr' => '/fr/contact/',           'es' => '/es/contacto/' ),
			'/faq/'                  => array( 'fr' => '/fr/faq/',               'es' => '/es/faq/' ),
			'/api-security/'         => array( 'fr' => '/fr/securite-api/',      'es' => '/es/seguridad-api/' ),
			'/legal/'                => array( 'fr' => '/fr/mentions-legales/',  'es' => '/es/aviso-legal/' ),
			'/appsumo-redeem/'       => array( 'fr' => '/fr/appsumo/',           'es' => '/es/appsumo/' ),
			'/fr/feuille-de-route/'  => array( 'en' => '/roadmap/',              'es' => '/es/hoja-de-ruta/' ),
			'/fr/visibilite-locale/' => array( 'en' => '/local-business/',       'es' => '/es/visibilidad-local/' ),
			'/fr/tarifs/'            => array( 'en' => '/pricing/',              'es' => '/es/precios/' ),
			'/fr/tarifs-agences/'    => array( 'en' => '/agency-pricing/',       'es' => '/es/precios-agencias/' ),
			'/fr/comment-ca-marche/' => array( 'en' => '/how-it-works/',         'es' => '/es/como-funciona/' ),
			'/fr/a-propos/'          => array( 'en' => '/about/',                'es' => '/es/sobre-nosotros/' ),
			'/fr/associations/'      => array( 'en' => '/nonprofits/',           'es' => '/es/organizaciones/' ),
			'/fr/programme-ambassadrice/'  => array( 'en' => '/ambassador-program/', 'es' => '/es/programa-embajadora/' ),
			'/fr/contact/'           => array( 'en' => '/contact/',              'es' => '/es/contacto/' ),
			'/fr/faq/'               => array( 'en' => '/faq/',                  'es' => '/es/faq/' ),
			'/fr/securite-api/'      => array( 'en' => '/api-security/',         'es' => '/es/seguridad-api/' ),
			'/fr/mentions-legales/'  => array( 'en' => '/legal/',               'es' => '/es/aviso-legal/' ),
			'/fr/appsumo/'           => array( 'en' => '/appsumo-redeem/',       'es' => '/es/appsumo/' ),
			'/es/hoja-de-ruta/'      => array( 'en' => '/roadmap/',              'fr' => '/fr/feuille-de-route/' ),
			'/es/visibilidad-local/' => array( 'en' => '/local-business/',       'fr' => '/fr/visibilite-locale/' ),
			'/es/precios/'           => array( 'en' => '/pricing/',              'fr' => '/fr/tarifs/' ),
			'/es/precios-agencias/'  => array( 'en' => '/agency-pricing/',       'fr' => '/fr/tarifs-agences/' ),
			'/es/como-funciona/'     => array( 'en' => '/how-it-works/',         'fr' => '/fr/comment-ca-marche/' ),
			'/es/sobre-nosotros/'    => array( 'en' => '/about/',                'fr' => '/fr/a-propos/' ),
			'/es/organizaciones/'    => array( 'en' => '/nonprofits/',           'fr' => '/fr/associations/' ),
			'/es/programa-embajadora/'     => array( 'en' => '/ambassador-program/', 'fr' => '/fr/programme-ambassadrice/' ),
			'/es/contacto/'          => array( 'en' => '/contact/',              'fr' => '/fr/contact/' ),
			'/es/faq/'               => array( 'en' => '/faq/',                  'fr' => '/fr/faq/' ),
			'/es/seguridad-api/'     => array( 'en' => '/api-security/',         'fr' => '/fr/securite-api/' ),
			'/es/aviso-legal/'       => array( 'en' => '/legal/',               'fr' => '/fr/mentions-legales/' ),
			'/es/appsumo/'           => array( 'en' => '/appsumo-redeem/',       'fr' => '/fr/appsumo/' ),
		);
		if ( isset( $slug_map[ $full ][ $code ] ) ) {
			return home_url( $slug_map[ $full ][ $code ] );
		}
		$p = preg_replace( '#^/(fr|es)(/?|/.+)$#', '$2', $full );
		if ( $p === '' ) { $p = '/'; }
		if ( isset( $slug_map[ $p ][ $code ] ) ) {
			return home_url( $slug_map[ $p ][ $code ] );
		}
		if ( $code === 'en' ) { return home_url( $p ); }
		return home_url( '/' . $code . ( $p === '/' ? '/' : $p ) );
	}
}

$b2c_slugs = array( 'local-business', 'local-audit', 'demo-dashboard', 'pricing', 'nonprofits' );
$post      = function_exists( 'get_queried_object' ) ? get_queried_object() : null;
$post_slug = ( $post instanceof \WP_Post ) ? (string) $post->post_name : '';
$is_b2c    = in_array( $post_slug, $b2c_slugs, true );

if ( $is_b2c ) {
	$cta_label = $current_lang === 'fr' ? 'Audit gratuit' : ( $current_lang === 'es' ? 'Auditoría gratis' : 'Start Free Audit' );
	$cta_url   = home_url( '/local-audit/' );
} else {
	$cta_label = $current_lang === 'fr' ? 'Accès agence' : ( $current_lang === 'es' ? 'Acceso agencia' : 'Agency Access' );
	$cta_url   = home_url( '/connexion/' );
}

$flags = array(
	'en' => array( 'class' => 'flag-us-uk', 'label' => 'EN' ),
	'fr' => array( 'class' => 'flag-fr', 'label' => 'FR' ),
	'es' => array( 'class' => 'flag-es', 'label' => 'ES' ),
);
$nav_items = array(
	array(
		'label' => $current_lang === 'fr' ? 'Agences' : ( $current_lang === 'es' ? 'Agencias' : 'Agencies' ),
		'url'   => home_url( $current_lang === 'fr' ? '/fr/' : ( $current_lang === 'es' ? '/es/' : '/' ) ),
		'match' => '#^$#',
	),
	array(
		'label' => $current_lang === 'fr' ? 'Entreprises locales' : ( $current_lang === 'es' ? 'Negocios locales' : 'Local Business' ),
		'url'   => home_url( $current_lang === 'fr' ? '/fr/visibilite-locale/' : ( $current_lang === 'es' ? '/es/visibilidad-local/' : '/local-business/' ) ),
		'match' => '#local-business#',
	),
	array(
		'label' => $current_lang === 'fr' ? 'Comment ça marche' : ( $current_lang === 'es' ? 'Cómo funciona' : 'How it works' ),
		'url'   => home_url( $current_lang === 'fr' ? '/fr/comment-ca-marche/' : ( $current_lang === 'es' ? '/es/como-funciona/' : '/how-it-works/' ) ),
		'match' => '#how-it-works#',
	),
	array(
		'label' => $current_lang === 'fr' ? 'Tarifs' : ( $current_lang === 'es' ? 'Precios' : 'Pricing' ),
		'url'   => home_url( $current_lang === 'fr' ? '/fr/tarifs/' : ( $current_lang === 'es' ? '/es/precios/' : '/pricing/' ) ),
		'match' => '#^pricing#',
	),
	array(
		'label' => $current_lang === 'fr'
			? 'Associations <span style="background:#DAA520;color:#0a1628;font-size:9px;font-weight:800;padding:2px 6px;border-radius:10px;letter-spacing:0.06em;text-transform:uppercase;vertical-align:middle;margin-left:4px;line-height:1.4;">GRATUIT</span>'
			: ( $current_lang === 'es'
				? 'ONG <span style="background:#DAA520;color:#0a1628;font-size:9px;font-weight:800;padding:2px 6px;border-radius:10px;letter-spacing:0.06em;text-transform:uppercase;vertical-align:middle;margin-left:4px;line-height:1.4;">GRATIS</span>'
				: 'Nonprofits <span style="background:#DAA520;color:#0a1628;font-size:9px;font-weight:800;padding:2px 6px;border-radius:10px;letter-spacing:0.06em;text-transform:uppercase;vertical-align:middle;margin-left:4px;line-height:1.4;">FREE</span>' ),
		'url'   => home_url(
			$current_lang === 'fr'
				? '/fr/associations/'
				: ( $current_lang === 'es'
					? '/es/organizaciones/'
					: '/nonprofits/' ) ),
		'match' => '#nonprofits|associations|organizaciones#',
		'raw'   => true,
	),
	array(
		'label' => 'Blog',
		'url'   => home_url( '/blog/' ),
		'match' => '#blog#',
	),
);
?>
<!DOCTYPE html>
<html lang="<?php echo esc_attr( $current_lang ); ?>">
<head>
<?php echo avi_get_layout( 'html-head' ); ?>
</head>
<body class="avi-has-nav avi-lang-<?php echo esc_attr( $current_lang ); ?>">
<style>
/* === BURGER MOBILE === */
@media (max-width: 768px) {
  .avi-top-nav .avi-nav-links { display: none !important; }
  .avi-top-nav .avi-nav-actions { display: none !important; }
  .avi-top-nav .avi-nav__burger { display: flex !important; }
  body.avi-has-nav { padding-top: 0 !important; }
}
@media (min-width: 769px) {
  .avi-top-nav .avi-nav__burger { display: none !important; }
  .avi-mobile-menu { display: none !important; }
}
.avi-top-nav .avi-nav__burger {
  display: none;
  flex-direction: column;
  gap: 5px;
  cursor: pointer;
  padding: 8px;
  background: none;
  border: none;
  z-index: 1001;
}
.avi-top-nav .avi-nav__burger span {
  display: block;
  width: 24px;
  height: 2px;
  background: #fff;
  border-radius: 2px;
  transition: all 0.3s;
}
.avi-mobile-menu {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #0A1628;
  z-index: 1000;
  flex-direction: column;
  padding: 80px 24px 40px;
  overflow-y: auto;
}
.avi-mobile-menu.is-open {
  display: flex !important;
}
.avi-mobile-menu__close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: none;
  border: none;
  color: #fff;
  font-size: 28px;
  cursor: pointer;
  padding: 8px;
}
.avi-mobile-menu__nav {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 32px;
}
.avi-mobile-menu__nav a {
  color: #fff;
  text-decoration: none;
  font-size: 1.1rem;
  font-weight: 600;
  padding: 12px 0;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}
.avi-mobile-menu__nav a.is-active {
  color: #F59E0B;
}
.avi-mobile-menu__actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 32px;
}
.avi-mobile-menu__login {
  color: rgba(255,255,255,0.7);
  text-decoration: none;
  font-size: 0.95rem;
  text-align: center;
  padding: 10px;
}
.avi-mobile-menu__flags {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: auto;
  padding-top: 24px;
  border-top: 1px solid rgba(255,255,255,0.08);
}
</style>
<header id="avi-nav" class="avi-top-nav" role="banner">
	<div class="avi-nav-wrap">
		<a class="avi-nav-brand" href="<?php echo esc_url( home_url( '/' ) ); ?>" aria-label="Avision">
			<img
				class="avision-header-logo-img"
				src="https://avision.org/wp-content/uploads/2026/03/avision-logo-png-70x70-1.png"
				alt="Avision"
				width="40"
				height="40"
				loading="eager"
			/>
		</a>
		<nav class="avi-nav-links" aria-label="Primary">
			<?php foreach ( $nav_items as $item ) : ?>
				<?php $item_active = preg_match( $item['match'], $current_path ) ? ' is-active' : ''; ?>
				<a class="avi-nav-link<?php echo esc_attr( $item_active ); ?>" href="<?php echo esc_url( $item['url'] ); ?>"><?php
				if ( ! empty( $item['raw'] ) ) {
					echo wp_kses( $item['label'], array(
						'span' => array( 'style' => array() ),
					) );
				} else {
					echo esc_html( $item['label'] );
				}
				?></a>
			<?php endforeach; ?>
		</nav>
		<button class="avi-nav__burger"
			id="avi-burger-btn"
			type="button"
			aria-label="Menu"
			aria-expanded="false">
			<span></span>
			<span></span>
			<span></span>
		</button>
		<div class="avi-nav-actions">
			<div class="avi-flag-list" aria-label="Language selector">
				<?php foreach ( $flags as $code => $flag ) : ?>
					<?php $active = $current_lang === $code ? ' is-active' : ''; ?>
					<a class="avi-flag-btn<?php echo esc_attr( $active ); ?>" href="<?php echo esc_url( avi_switch_lang_url( (string) $code ) ); ?>" aria-label="<?php echo esc_attr( (string) $flag['label'] ); ?>" hreflang="<?php echo esc_attr( (string) $code ); ?>">
						<span class="flag-icon <?php echo esc_attr( (string) $flag['class'] ); ?>" aria-hidden="true"></span>
					</a>
				<?php endforeach; ?>
			</div>
			<a href="<?php echo esc_url( home_url( '/connexion/' ) ); ?>" class="avi-nav__login">
				<?php echo esc_html( $current_lang === 'fr' ? 'Connexion' : ( $current_lang === 'es' ? 'Acceder' : 'Login' ) ); ?>
			</a>
			<a href="<?php echo esc_url( $cta_url ); ?>" class="avi-btn avi-btn--primary avi-btn--sm avi-nav__cta">
				<?php echo esc_html( $cta_label ); ?> →
			</a>
		</div>
	</div>
</header>

<div class="avi-mobile-menu" id="avi-mobile-menu" role="dialog" aria-modal="true">
	<button type="button" class="avi-mobile-menu__close" id="avi-menu-close" aria-label="Fermer">✕</button>

	<nav class="avi-mobile-menu__nav">
		<?php foreach ( $nav_items as $item ) : ?>
			<?php $item_active = preg_match( $item['match'], $current_path ) ? ' is-active' : ''; ?>
			<a href="<?php echo esc_url( $item['url'] ); ?>"
				class="<?php echo esc_attr( $item_active ); ?>">
				<?php
				if ( ! empty( $item['raw'] ) ) {
					echo wp_kses( $item['label'], array(
						'span' => array( 'style' => array() ),
					) );
				} else {
					echo esc_html( $item['label'] );
				}
				?>
			</a>
		<?php endforeach; ?>
	</nav>

	<div class="avi-mobile-menu__actions">
		<a href="<?php echo esc_url( $cta_url ); ?>"
			class="avi-btn avi-btn--primary">
			<?php echo esc_html( $cta_label ); ?> →
		</a>
		<a href="<?php echo esc_url( home_url( '/connexion/' ) ); ?>"
			class="avi-mobile-menu__login">
			<?php echo esc_html( $current_lang === 'fr' ? 'Connexion' : ( $current_lang === 'es' ? 'Acceder' : 'Login' ) ); ?>
		</a>
	</div>

	<div class="avi-mobile-menu__flags">
		<?php foreach ( $flags as $code => $flag ) : ?>
			<?php $active = $current_lang === $code ? ' is-active' : ''; ?>
			<a href="<?php echo esc_url( avi_switch_lang_url( (string) $code ) ); ?>"
				hreflang="<?php echo esc_attr( (string) $code ); ?>"
				aria-label="<?php echo esc_attr( (string) $flag['label'] ); ?>"
				style="<?php echo $current_lang === $code ? 'opacity:1' : 'opacity:0.5'; ?>">
				<span class="flag-icon <?php echo esc_attr( (string) $flag['class'] ); ?>"></span>
			</a>
		<?php endforeach; ?>
	</div>
</div>

<script>
(function() {
  var btn = document.getElementById('avi-burger-btn');
  var menu = document.getElementById('avi-mobile-menu');
  var close = document.getElementById('avi-menu-close');
  if (!btn || !menu) return;
  btn.addEventListener('click', function() {
    menu.classList.add('is-open');
    btn.setAttribute('aria-expanded', 'true');
    document.body.style.overflow = 'hidden';
  });
  function closeMenu() {
    menu.classList.remove('is-open');
    btn.setAttribute('aria-expanded', 'false');
    document.body.style.overflow = '';
  }
  if (close) close.addEventListener('click', closeMenu);
  menu.addEventListener('click', function(e) {
    if (e.target === menu) closeMenu();
  });
  document.addEventListener('keydown', function(e) {
    if (e.key === 'Escape') closeMenu();
  });
  menu.querySelectorAll('a').forEach(function(a) {
    a.addEventListener('click', closeMenu);
  });
})();
</script>
