<?php
/**
 * The header for our theme
 *
 * @package ChaletMontagne
 * @since 1.0.0
 */
?>
<!doctype html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="profile" href="https://gmpg.org/xfn/11">
    <?php wp_head(); ?>
</head>

<body <?php body_class(); ?> <?php chaletmontagne_body_attributes(); ?>>
<?php wp_body_open(); ?>

<div id="page" class="site">
    <a class="skip-link screen-reader-text" href="#primary">
        <?php esc_html_e('Aller au contenu principal', 'chalet-montagne'); ?>
    </a>

    <header id="masthead" class="site-header">
        <div class="site-container">
            <div class="header-top">
                <div class="site-branding">
                    <?php
                    // Custom logo
                    if (has_custom_logo()) :
                        the_custom_logo();
                    else :
                        ?>
                        <div class="site-branding-text">
                            <?php if (is_front_page() && is_home()) : ?>
                                <h1 class="site-title">
                                    <a href="<?php echo esc_url(home_url('/')); ?>" rel="home">
                                        <?php bloginfo('name'); ?>
                                    </a>
                                </h1>
                            <?php else : ?>
                                <p class="site-title">
                                    <a href="<?php echo esc_url(home_url('/')); ?>" rel="home">
                                        <?php bloginfo('name'); ?>
                                    </a>
                                </p>
                            <?php endif; ?>

                            <?php
                            $description = get_bloginfo('description', 'display');
                            if ($description || is_customize_preview()) :
                                ?>
                                <p class="site-description"><?php echo esc_html($description); ?></p>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </div><!-- .site-branding -->
            </div><!-- .header-top -->

            <nav id="site-navigation" class="main-navigation">
                <?php
                wp_nav_menu(array(
                    'theme_location' => 'primary',
                    'menu_id'        => 'primary-menu',
                    'container'      => false,
                    'fallback_cb'    => false,
                ));
                ?>
            </nav><!-- #site-navigation -->
        </div><!-- .site-container -->
    </header><!-- #masthead -->

    <?php
    // Hero section on front page (uniquement si pas de template luxury)
    $page_template = get_page_template_slug();
    $is_luxury_template = in_array($page_template, array(
        'page-templates/template-home-luxury.php',
        'page-templates/template-about-luxury.php',
        'page-templates/template-contact-luxury.php'
    ));

    if (is_front_page() && !is_home() && !$is_luxury_template) :
        $hero_title = get_theme_mod('chaletmontagne_hero_title', __('Locations de charme à la montagne', 'chalet-montagne'));
        $hero_subtitle = get_theme_mod('chaletmontagne_hero_subtitle', __('Des hébergements d\'exception pour vos séjours à la montagne', 'chalet-montagne'));
        $hero_cta_text = get_theme_mod('chaletmontagne_hero_cta_text', __('Découvrir nos locations', 'chalet-montagne'));
        $hero_cta_url = get_theme_mod('chaletmontagne_hero_cta_url');

        // Check if slider is enabled
        $slider_enabled = get_theme_mod('chaletmontagne_slider_enable', false);
        $slider_images = get_theme_mod('chaletmontagne_slider_images', '');

        if ($slider_enabled && !empty($slider_images)) :
            // Slider mode
            $slider_image_ids = array_filter(array_map('absint', explode(',', $slider_images)));
            $slider_overlay = get_theme_mod('chaletmontagne_slider_overlay', 40);

            if (!empty($slider_image_ids) || is_customize_preview()) :
                ?>
                <section class="hero-slider" style="--slider-overlay: <?php echo esc_attr($slider_overlay); ?>;" aria-label="<?php esc_attr_e('Diaporama d\'images', 'chalet-montagne'); ?>">
                    <div class="hero-slider-container">
                        <?php foreach ($slider_image_ids as $index => $image_id) :
                            $image_url = wp_get_attachment_image_url($image_id, 'full');
                            $image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true);
                            if ($image_url) :
                                ?>
                                <div class="hero-slide<?php echo $index === 0 ? ' active' : ''; ?>" aria-hidden="<?php echo $index === 0 ? 'false' : 'true'; ?>">
                                    <img src="<?php echo esc_url($image_url); ?>"
                                         alt="<?php echo esc_attr($image_alt ?: sprintf(__('Slide %d', 'chalet-montagne'), $index + 1)); ?>"
                                         class="hero-slide-image"
                                         loading="<?php echo $index === 0 ? 'eager' : 'lazy'; ?>">
                                    <div class="hero-slide-overlay"></div>
                                </div>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </div>

                    <?php if ($hero_title || $hero_subtitle || is_customize_preview()) : ?>
                        <div class="hero-slider-content">
                            <?php if ($hero_title) : ?>
                                <h1 class="hero-title"><?php echo nl2br(esc_html($hero_title)); ?></h1>
                            <?php endif; ?>

                            <?php if ($hero_subtitle) : ?>
                                <p class="hero-subtitle"><?php echo esc_html($hero_subtitle); ?></p>
                            <?php endif; ?>

                            <?php if ($hero_cta_url && $hero_cta_text) : ?>
                                <a href="<?php echo esc_url($hero_cta_url); ?>" class="hero-cta button">
                                    <?php echo esc_html($hero_cta_text); ?>
                                </a>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </section>
            <?php endif; ?>

        <?php else :
            // Single image mode (original behavior)
            $hero_bg_id = get_theme_mod('chaletmontagne_hero_bg');
            $hero_bg_url = $hero_bg_id ? wp_get_attachment_image_url($hero_bg_id, 'full') : '';

            $hero_overlay_opacity = get_theme_mod('chaletmontagne_hero_overlay_opacity', 70);

            if ($hero_title || $hero_subtitle || is_customize_preview()) :
                ?>
                <section class="hero-section" style="<?php if ($hero_bg_url) echo 'background-image: url(' . esc_url($hero_bg_url) . ');'; ?> --hero-overlay-opacity: <?php echo esc_attr($hero_overlay_opacity / 100); ?>;">
                    <div class="hero-overlay"></div>
                    <div class="hero-content">
                        <?php if ($hero_title) : ?>
                            <h1 class="hero-title"><?php echo nl2br(esc_html($hero_title)); ?></h1>
                        <?php endif; ?>

                        <?php if ($hero_subtitle) : ?>
                            <p class="hero-subtitle"><?php echo esc_html($hero_subtitle); ?></p>
                        <?php endif; ?>

                        <?php if ($hero_cta_url && $hero_cta_text) : ?>
                            <a href="<?php echo esc_url($hero_cta_url); ?>" class="hero-cta button">
                                <?php echo esc_html($hero_cta_text); ?>
                            </a>
                        <?php endif; ?>
                    </div>
                </section>
            <?php endif; ?>
        <?php endif; ?>
    <?php endif; ?>
