<?php
/**
 * The template for displaying comments.
 *
 * This template displays the comment list and comment form.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 * @package leatherhouse
 */

// Prevents loading comments if the post is password protected.
if (post_password_required()) {
    return;
}
?>

<div class="xl-column-12">
    <div id="comments" class="comments-area">

        <?php 
        // Check if there are comments to display
        if (have_comments()) : ?>
            <div class="single-comments-title">
                <h2>
                    <?php
                    // Display comment count with proper singular/plural handling
                    printf(
                        esc_html(
                            _n('One thought on &ldquo;%2$s&rdquo;', '%1$s thoughts on &ldquo;%2$s&rdquo;', get_comments_number(), 'leather-house')
                        ),
                        number_format_i18n(get_comments_number()), // Format the comment count
                        '<span>' . esc_html(get_the_title()) . '</span>' // Display the post title
                    );
                    ?>
                </h2>
            </div>

            <?php
            // Check if comment navigation is required (multiple comment pages exist)
            $comment_pagination = get_comment_pages_count() > 1 && get_option('page_comments');

            if ($comment_pagination) :
            ?>
                <nav class="comment-navigation">
                    <h2 class="screen-reader-text"><?php esc_html_e('Comment navigation', 'leather-house'); ?></h2>
                    <div class="nav-links">
                        <div class="nav-previous"><?php previous_comments_link(esc_html__('Older Comments', 'leather-house')); ?></div>
                        <div class="nav-next"><?php next_comments_link(esc_html__('Newer Comments', 'leather-house')); ?></div>
                    </div>
                </nav>
            <?php endif; ?>

            <!-- Comment list -->
            <ol class="comment-list">
                <?php
                // Display comments using WordPress' built-in function
                wp_list_comments(array(
                    'style'      => 'ol', // Ordered list format
                    'short_ping' => true, // Shorten pingbacks/trackbacks
                    'avatar_size'=> 64    // Set avatar size for comment authors
                ));
                ?>
            </ol>

            <?php 
            // Display another comment navigation at the bottom
            if ($comment_pagination) : ?>
                <nav class="comment-navigation">
                    <h2 class="screen-reader-text"><?php esc_html_e('Comment navigation', 'leather-house'); ?></h2>
                    <div class="nav-links">
                        <div class="nav-previous"><?php previous_comments_link(esc_html__('Older Comments', 'leather-house')); ?></div>
                        <div class="nav-next"><?php next_comments_link(esc_html__('Newer Comments', 'leather-house')); ?></div>
                    </div>
                </nav>
            <?php endif; ?>

        <?php endif; ?>

        <?php
        // If comments are closed but existing comments are present, display a message
        if (!comments_open() && get_comments_number() && post_type_supports(get_post_type(), 'comments')) :
        ?>
            <p class="no-comments"><?php esc_html_e('Comments are closed.', 'leather-house'); ?></p>
        <?php endif; ?>

        <!-- Display the comment form -->
        <?php comment_form(); ?>

    </div>
</div>
