<?php
/**
 * The template for displaying the footer
 *
 * @package ChaletMontagne
 * @since 1.0.0
 */

$phone   = get_theme_mod('chaletmontagne_phone');
$email   = get_theme_mod('chaletmontagne_email');
$address = get_theme_mod('chaletmontagne_address');
$hours   = get_theme_mod('chaletmontagne_hours');
$has_contact = $phone || $email || $address || $hours;

// Check if social links exist
$social_networks = array('facebook', 'instagram', 'twitter', 'youtube', 'chalet-montagne', 'airbnb', 'booking');
$has_social = false;
foreach ($social_networks as $network) {
    if (get_theme_mod("chaletmontagne_social_{$network}")) {
        $has_social = true;
        break;
    }
}

// Footer widget mode: at least one footer widget zone has content
$has_footer_widgets = is_active_sidebar('footer-1') || is_active_sidebar('footer-2') || is_active_sidebar('footer-3');

// Customizer options
$custom_copyright  = get_theme_mod('chaletmontagne_footer_copyright', '');
$hide_theme_credit = get_theme_mod('chaletmontagne_hide_theme_credit', false);
?>

    <footer id="colophon" class="site-footer">
        <div class="footer-top-bar"></div>

        <div class="site-container">
            <?php if ($has_footer_widgets) : ?>
                <?php
                // Count active columns for grid class
                $col_count = 0;
                for ($i = 1; $i <= 3; $i++) {
                    if (is_active_sidebar("footer-{$i}")) {
                        $col_count++;
                    }
                }
                ?>
                <div class="footer-widgets">
                    <div class="footer-widget-area footer-cols-<?php echo esc_attr($col_count); ?>">
                        <?php for ($i = 1; $i <= 3; $i++) : ?>
                            <?php if (is_active_sidebar("footer-{$i}")) : ?>
                                <div class="footer-widget">
                                    <?php dynamic_sidebar("footer-{$i}"); ?>
                                </div>
                            <?php endif; ?>
                        <?php endfor; ?>
                    </div>
                </div>

            <?php elseif ($has_contact || $has_social) : ?>
                <div class="footer-widgets">
                    <div class="footer-widget-area">
                        <?php if ($has_contact) : ?>
                        <div class="footer-widget">
                            <h3 class="widget-title"><?php esc_html_e('Contact', 'chalet-montagne'); ?></h3>
                            <?php if ($phone) : ?>
                                <p class="footer-phone">
                                    <a href="tel:<?php echo esc_attr(str_replace(' ', '', $phone)); ?>">
                                        <?php echo esc_html($phone); ?>
                                    </a>
                                </p>
                            <?php endif; ?>
                            <?php if ($email) : ?>
                                <p class="footer-email">
                                    <a href="mailto:<?php echo esc_attr($email); ?>">
                                        <?php echo esc_html($email); ?>
                                    </a>
                                </p>
                            <?php endif; ?>
                            <?php if ($address) : ?>
                                <p class="footer-address"><?php echo nl2br(esc_html($address)); ?></p>
                            <?php endif; ?>
                            <?php if ($hours) : ?>
                                <p class="footer-hours"><?php echo esc_html($hours); ?></p>
                            <?php endif; ?>
                        </div>
                        <?php endif; ?>

                        <?php if ($has_social) : ?>
                        <div class="footer-widget">
                            <h3 class="widget-title"><?php esc_html_e('Suivez-nous', 'chalet-montagne'); ?></h3>
                            <?php chaletmontagne_social_links(); ?>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endif; ?>

            <div class="site-info">
                <div class="footer-text">
                    <p>
                        <?php
                        if ($custom_copyright) {
                            echo esc_html($custom_copyright);
                        } else {
                            printf(
                                esc_html__('&copy; %1$s %2$s. Tous droits réservés.', 'chalet-montagne'),
                                esc_html(get_bloginfo('name')),
                                wp_date('Y')
                            );
                        }
                        ?>
                    </p>

                    <?php
                    // Liens légaux
                    $legal_links = array();

                    $mentions_page_id = get_theme_mod('chaletmontagne_mentions_legales_page');
                    // Polylang/WPML: resolve translated page ID for the current language.
                    // The 'wpml_object_id' filter is implemented by both plugins.
                    $mentions_page_id = apply_filters('wpml_object_id', $mentions_page_id, 'page', true);
                    if ($mentions_page_id) {
                        $legal_links[] = '<a href="' . esc_url(get_permalink($mentions_page_id)) . '">' . esc_html__('Mentions légales', 'chalet-montagne') . '</a>';
                    }

                    $privacy_page_id = get_theme_mod('chaletmontagne_privacy_policy_page');
                    $privacy_page_id = apply_filters('wpml_object_id', $privacy_page_id, 'page', true);
                    if ($privacy_page_id) {
                        $legal_links[] = '<a href="' . esc_url(get_permalink($privacy_page_id)) . '">' . esc_html__('Politique de confidentialité', 'chalet-montagne') . '</a>';
                    } elseif (get_privacy_policy_url()) {
                        $legal_links[] = '<a href="' . esc_url(get_privacy_policy_url()) . '">' . esc_html__('Politique de confidentialité', 'chalet-montagne') . '</a>';
                    }

                    if (!empty($legal_links)) :
                    ?>
                        <p class="footer-legal">
                            <?php echo implode(' <span class="legal-separator">|</span> ', $legal_links); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
                        </p>
                    <?php endif; ?>

                    <?php if (!$hide_theme_credit) : ?>
                    <p class="theme-credit">
                        <?php
                        printf(
                            esc_html__('Thème fourni par %s', 'chalet-montagne'),
                            '<a href="https://www.chalet-montagne.com" target="_blank" rel="noopener">chalet-montagne.com</a>'
                        );
                        ?>
                    </p>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </footer>
</div><!-- #page -->

<?php wp_footer(); ?>

</body>
</html>
