<?php
/**
 * The template for displaying comments
 *
 * @package Pawfect
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

/*
 * If the current post is protected by a password and
 * the visitor has not yet entered the password we will
 * return early without loading the comments.
 */
if (post_password_required()) {
    return;
}
?>

    <?php if (have_comments()): ?>
        <h2 class="pawfect-font-serif pawfect-text-3xl md:pawfect-text-4xl pawfect-mb-8">
            <?php
            $comments_number = get_comments_number();
            echo number_format_i18n($comments_number) . ' ' . _n('Comment', 'Comments', $comments_number, 'pawfect');
            ?>
        </h2>

        <!-- Comment List -->
        <ul class="comment-list pawfect-space-y-8">
            <?php
            wp_list_comments(array(
                'style'       => 'ul',
                'short_ping'  => true,
                'avatar_size' => 64,
                'callback'    => 'pawfect_comment_callback',
            ));
            ?>
        </ul>

        <?php
        // Comment pagination
        if (get_comment_pages_count() > 1 && get_option('page_comments')):
        ?>
            <nav class="pawfect-comment-navigation pawfect-flex pawfect-justify-between pawfect-mt-8">
                <div class="pawfect-nav-previous">
                    <?php previous_comments_link(esc_html__('&larr; Older Comments', 'pawfect')); ?>
                </div>
                <div class="pawfect-nav-next">
                    <?php next_comments_link(esc_html__('Newer Comments &rarr;', 'pawfect')); ?>
                </div>
            </nav>
        <?php endif; ?>

    <?php endif; // Check for have_comments() ?>

    <?php
    // If comments are closed and there are comments, let's leave a little note
    if (!comments_open() && get_comments_number() && post_type_supports(get_post_type(), 'comments')):
    ?>
        <p class="pawfect-no-comments pawfect-text-gray-500 pawfect-italic pawfect-mt-8">
            <?php esc_html_e('Comments are closed.', 'pawfect'); ?>
        </p>
    <?php endif; ?>

    <?php
    // Comment Form matching the HTML design
    $commenter = wp_get_current_commenter();
    $comment_form_args = array(
        'title_reply'          => esc_html__('Leave a Reply', 'pawfect'),
        'title_reply_to'       => esc_html__('Leave a Reply to %s', 'pawfect'),
        'cancel_reply_link'    => esc_html__('Cancel Reply', 'pawfect'),
        'label_submit'         => esc_html__('Post Comment', 'pawfect'),
        'submit_button'        => '<div><button type="submit" class="pawfect-btn">%4$s</button></div>',
        'comment_field'        => '<div><label for="comment" class="pawfect-block pawfect-mb-2 pawfect-font-bold">' . esc_html__('Comment', 'pawfect') . ' *</label><textarea id="comment" name="comment" rows="6" class="pawfect-w-full pawfect-p-4 pawfect-border pawfect-border-border-color pawfect-rounded-md focus:pawfect-outline-none focus:pawfect-ring-2 focus:pawfect-ring-accent" required></textarea></div>',
        'fields'               => array(
            'author' => '<div class="pawfect-grid pawfect-grid-cols-1 md:pawfect-grid-cols-2 pawfect-gap-6"><div><label for="author" class="pawfect-block pawfect-mb-2 pawfect-font-bold">' . esc_html__('Name', 'pawfect') . ' *</label><input type="text" id="author" name="author" value="' . esc_attr($commenter['comment_author']) . '" class="pawfect-w-full pawfect-p-4 pawfect-border pawfect-border-border-color pawfect-rounded-md focus:pawfect-outline-none focus:pawfect-ring-2 focus:pawfect-ring-accent" required></div>',
            'email'  => '<div><label for="email" class="pawfect-block pawfect-mb-2 pawfect-font-bold">' . esc_html__('Email', 'pawfect') . ' *</label><input type="email" id="email" name="email" value="' . esc_attr($commenter['comment_author_email']) . '" class="pawfect-w-full pawfect-p-4 pawfect-border pawfect-border-border-color pawfect-rounded-md focus:pawfect-outline-none focus:pawfect-ring-2 focus:pawfect-ring-accent" required></div></div>',
        ),
        'class_container'      => 'pawfect-comment-respond',
        'class_form'           => 'pawfect-comment-form pawfect-space-y-6',
        'title_reply_before'   => '<div class="pawfect-mt-16 pawfect-pt-12 pawfect-border-t pawfect-border-border-color"><h3 class="pawfect-font-serif pawfect-text-3xl md:pawfect-text-4xl pawfect-mb-4">',
        'title_reply_after'    => '</h3><p class="pawfect-text-text-light pawfect-mb-8">' . esc_html__('Your email address will not be published. Required fields are marked *', 'pawfect') . '</p></div>',
        'comment_notes_before' => '',
        'comment_notes_after'  => '',
    );

    comment_form($comment_form_args);
    ?>

</div><!-- #comments -->

<?php

?>
