<?php
// Layout 1: Make category and author clickable
function get_layout_1_markup($query, $posts_per_row) {
    ob_start();

    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            $col_class = 'rswpthemes-awt-grid-col-md-' . (12 / $posts_per_row);
            $featured_image = get_the_post_thumbnail_url(get_the_ID(), 'medium');
            $excerpt = get_the_excerpt();
            $categories = get_the_category();
            $author_id = get_the_author_meta('ID');
            $author_name = get_the_author_meta('display_name', $author_id);
            $author_avatar = get_avatar_url($author_id, array('size' => 40));
            $post_date = get_the_date('M j, Y');
            $read_time = '3 min read';
            ?>
            <div class="<?php echo esc_attr($col_class); ?> mb-4">
                <div class="rswpthemes-awt-filterable-post-card">
                    <?php if ($featured_image) : ?>
                        <div class="rswpthemes-awt-filterable-post-card-image" style="background-image: url('<?php echo esc_url($featured_image); ?>');"></div>
                    <?php endif; ?>
                    <div class="rswpthemes-awt-filterable-post-card-content">
                        <h2 class="post-title">
                            <a href="<?php echo esc_url(get_permalink()); ?>" class="post-title-link" data-post-id="<?php echo esc_attr(get_the_ID()); ?>">
                                <?php the_title(); ?>
                            </a>
                        </h2>
                        <p class="post-excerpt"><?php echo esc_html(wp_trim_words($excerpt, 20)); ?></p>
                        <div class="post-tags">
                            <?php foreach ($categories as $category) : ?>
                                <span class="tag filter-category" data-category-id="<?php echo esc_attr($category->term_id); ?>">
                                    <?php echo esc_html($category->name); ?>
                                </span>
                            <?php endforeach; ?>
                        </div>
                        <div class="post-meta">
                            <img src="<?php echo esc_url($author_avatar); ?>" alt="<?php echo esc_attr($author_name); ?>" class="author-avatar">
                            <span class="author-name filter-author" data-author-id="<?php echo esc_attr($author_id); ?>">
                                <?php echo esc_html($author_name); ?>
                            </span>
                            <span class="post-date"><?php echo esc_html($post_date); ?> — <?php echo esc_html($read_time); ?></span>
                        </div>
                    </div>
                </div>
            </div>
            <?php
        }

        $output = ob_get_clean();
        $max_pages = $query->max_num_pages;

        return array(
            'html' => $output,
            'max_pages' => $max_pages
        );
    } else {
        $output = ob_get_clean();
        ?>
        <div class="rswpthemes-awt-grid-col-12">
            <p>No posts found.</p>
        </div>
        <?php
        $output = ob_get_clean();

        return array(
            'html' => $output,
            'max_pages' => 0
        );
    }
}


// Layout 2: Make category and author clickable
function get_layout_2_markup($query, $posts_per_row) {
    ob_start();

    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            $col_class = 'rswpthemes-awt-grid-col-md-' . (12 / $posts_per_row);
            $featured_image = get_the_post_thumbnail_url(get_the_ID(), 'medium');
            $excerpt = get_the_excerpt();
            $categories = get_the_category();
            $author_id = get_the_author_meta('ID');
            $author_name = get_the_author_meta('display_name', $author_id);
            $post_date = get_the_date('M j, Y');
            $read_time = '3 min read';
            $is_premium = false;
            ?>
            <div class="<?php echo esc_attr($col_class); ?> mb-4">
                <div class="rswpthemes-awt-filterable-post-card layout-2">
                    <?php if ($featured_image) : ?>
                        <div class="rswpthemes-awt-filterable-post-card-image" style="background-image: url('<?php echo esc_url($featured_image); ?>');">
                            <?php if (!empty($categories)) : ?>
                                <span class="post-category-tag filter-category" data-category-id="<?php echo esc_attr($categories[0]->term_id); ?>">
                                    <?php echo esc_html($categories[0]->name); ?>
                                </span>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                    <div class="rswpthemes-awt-filterable-post-card-content">
                        <h2 class="post-title">
                            <a href="<?php echo esc_url(get_permalink()); ?>" class="post-title-link" data-post-id="<?php echo esc_attr(get_the_ID()); ?>">
                                <?php the_title(); ?>
                            </a>
                        </h2>
                        <p class="post-excerpt"><?php echo esc_html(wp_trim_words($excerpt, 20)); ?></p>
                        <div class="post-meta">
                            <span class="author-name filter-author" data-author-id="<?php echo esc_attr($author_id); ?>">
                                <?php echo esc_html($author_name); ?>
                            </span>
                            <span class="post-date"><?php echo esc_html($post_date); ?></span>
                            <span class="read-time"><?php echo esc_html($read_time); ?></span>
                            <?php if ($is_premium) : ?>
                                <span class="premium-lock">🔒</span>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
            <?php
        }

        $output = ob_get_clean();
        $max_pages = $query->max_num_pages;

        return array(
            'html' => $output,
            'max_pages' => $max_pages
        );
    } else {
        $output = ob_get_clean();
        ?>
        <div class="rswpthemes-awt-grid-col-12">
            <p>No posts found.</p>
        </div>
        <?php
        $output = ob_get_clean();

        return array(
            'html' => $output,
            'max_pages' => 0
        );
    }
}
