<?php
// Recommended Posts Section For Reader
function book_review_blog_cats_related_post() {
    $post_id = get_the_ID();
    $cat_ids = array();
    $categories = get_the_category($post_id);

    // Collect category IDs
    if (is_array($categories) && !empty($categories)) {
        foreach ($categories as $category) {
            $cat_ids[] = $category->term_id;
        }
    }

    $current_post_type = get_post_type($post_id);

    // Configuration options
    $settings = array(
        'posts_per_page' => get_theme_mod('related_posts_count', 3),
        'order' => get_theme_mod('related_posts_order', 'rand'), // Options: date, rand, title
        'show_post_category' => get_theme_mod('show_related_post_category', true),
        'show_post_thumbnail' => get_theme_mod('show_related_post_thumbnail', true),
        'show_post_date' => get_theme_mod('show_related_post_date', true),
        'show_post_author' => get_theme_mod('show_related_post_author', true),
        'show_post_title' => get_theme_mod('show_related_post_title', true),
        'show_post_excerpt' => get_theme_mod('show_related_post_excerpt', true),
        'excerpt_length' => get_theme_mod('related_post_excerpt_length', 20),
        'show_read_more' => get_theme_mod('show_related_post_read_more', true),
        'read_more_text' => get_theme_mod('related_post_read_more_text', __('Read More', 'book-review-blog')),
        'section_title' => get_theme_mod('related_posts_section_title', __('Related Posts', 'book-review-blog')),
    );

    // Query arguments
    $query_args = array(
        'category__in' => $cat_ids,
        'post_type' => $current_post_type,
        'post__not_in' => array($post_id),
        'posts_per_page' => $settings['posts_per_page'],
        'orderby' => $settings['order'],
        'ignore_sticky_posts' => 1,
    );

    // Determine column classes based on sidebar layout
    $sidebar_layout = get_theme_mod('post_sidebar', 'right');
    $column_class = 'col-12 col-md-6 col-lg-4 blog-grid-layout';

    switch ($sidebar_layout) {
        case 'no':
            $column_class = 'col-12 col-md-6 col-lg-4 blog-grid-layout';
            break;
        case 'right':
        case 'left':
            $column_class = 'col-12 col-md-6 col-lg-6 blog-grid-layout';
            break;
        case 'both':
            $column_class = 'col-12 blog-grid-layout';
            break;
    }

    $related_query = new WP_Query($query_args);

    if ($related_query->have_posts()) : ?>
        <section class="recommended-post-section mt-5 mb-5">
            <h4 class="related-post-title text-left"><?php echo esc_html($settings['section_title']); ?></h4>
            <div class="related-post-slider row justify-content-md-center">
                <?php while ($related_query->have_posts()) : $related_query->the_post();
                    $post_classes = 'book-review-blog-standard-post';
                    if (!has_post_thumbnail()) {
                        $post_classes .= ' no-post-thumbnail';
                    }
                    $post_classes = apply_filters('book_review_blog_article_tag_classes', $post_classes);
                ?>
                    <div class="<?php echo esc_attr($column_class); ?>">
                        <article id="post-<?php the_ID(); ?>" <?php post_class($post_classes); ?>>
                            <div class="book-review-blog-standard-post__entry-content text-left">
                                <?php if (has_post_thumbnail() && $settings['show_post_thumbnail']) : ?>
                                    <div class="book-review-blog-standard-post__thumbnail post-header">
                                        <?php
                                        the_post_thumbnail('medium', array(
                                            'class' => 'img-fluid',
                                            'loading' => 'lazy',
                                            'alt' => get_the_title()
                                        ));
                                        ?>
                                    </div>
                                <?php endif; ?>

                                <div class="book-review-blog-standard-post__content-wrapper">
                                    <?php if ($settings['show_post_category']) : ?>
                                        <div class="book-review-blog-standard-post__post-meta mb-3">
                                            <?php book_review_blog_categories(); ?>
                                        </div>
                                    <?php endif; ?>

                                    <?php if ($settings['show_post_title']) : ?>
                                        <div class="book-review-blog-standard-post__post-title">
                                            <h2 class="h4"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                                        </div>
                                    <?php endif; ?>

                                    <?php if ($settings['show_post_author'] || $settings['show_post_date']) : ?>
                                        <div class="book-review-blog-standard-post__blog-meta d-flex gap-3 mb-3">
                                            <?php
                                            if ($settings['show_post_author']) {
                                                book_review_blog_posted_by(true);
                                            }
                                            if ($settings['show_post_date']) {
                                                book_review_blog_posted_on(true);
                                            }
                                            ?>
                                        </div>
                                    <?php endif; ?>

                                    <?php if ($settings['show_post_excerpt']) : ?>
                                        <div class="book-review-blog-standard-post__post-excerpt">
                                            <?php echo wp_trim_words(get_the_excerpt(), $settings['excerpt_length'], '...'); ?>
                                        </div>
                                    <?php endif; ?>

                                    <?php if ($settings['show_read_more']) : ?>
                                        <div class="book-review-blog-standard-post__read-more mt-3">
                                            <a href="<?php the_permalink(); ?>" class="btn btn-outline-primary btn-sm">
                                                <?php echo esc_html($settings['read_more_text']); ?>
                                            </a>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </article>
                    </div>
                <?php endwhile; ?>
            </div>
        </section>
    <?php else : ?>
        <section class="recommended-post-section mt-5 mb-5">
            <h4 class="related-post-title text-left"><?php echo esc_html($settings['section_title']); ?></h4>
            <p><?php esc_html_e('No related posts found.', 'book-review-blog'); ?></p>
        </section>
    <?php endif;

    wp_reset_postdata();
}
