<?php 

function allNews_shortcode($atts) {
    $atts = shortcode_atts(
        array(
            // Define any attributes for the shortcode if needed
        ),
        $atts,
        'news'
    );

    ob_start();

    $news_args = array(
        'post_type'      => array('post', 'downloads'),
        'posts_per_page' => 12,
        'tax_query'      => array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms'    => array('news', 'blog'),
            ),
            array(
                'taxonomy' => 'file_category',
                'field'    => 'slug',
                'terms'    => array('annual-reports', 'newsletters', 'press-statements', 'petitions', 'proceedings-reports', 'advocacy-letters', 'memoranda', 'policy-briefing-papers', 'communiques', 'case-files', 'other-publications'),
            ),
        ),
    );

    $news_query = new WP_Query($news_args);

    if ($news_query->have_posts()) :
        ?>
        <div class="row mb-md-5 mb-3">
            <?php
            while ($news_query->have_posts()) :
                $news_query->the_post();
                $categories = get_the_category();
                $file_categories = get_the_terms(get_the_ID(), 'file_category');
                ?>
                <div class="col-xl-4 col-lg-6 col-sm-6 g-md-5 g-3">
                    <article>
                        <a href="<?= esc_url(get_permalink()); ?>">
                            <div class="card shadow-sm  border-0" data-aos="fade-up">
                                <div class="post-thumbnail">
                                    <?php
                                    // Display the post thumbnail
                                    if (has_post_thumbnail()) {
                                        the_post_thumbnail(); 
                                    } else {
                                        echo '<img src="' . get_template_directory_uri() . '/assets/images/placeholder-image.jpg" alt="Afiego" class="img-fluid"/>';
                                    }
                                    ?>
                                 </div><!-- Thumbnail !-->
                                <div class="card-body news-details">
                                    <div class="catz">
                                        <?php
                                        // Display categories
                                        if ($categories) {
                                            foreach ($categories as $category) {
                                                echo esc_html($category->name);
                                            }
                                        }

                                        // Display file categories
                                        if ($file_categories && !is_wp_error($file_categories)) {
                                            foreach ($file_categories as $file_category) {
                                                echo esc_html($file_category->name);
                                            }
                                        }
                                        ?>
                                    </div><!-- Categories !-->
                                    <h3><?php the_title(); ?></h3>
                                </div><!-- card body !-->
                            </div><!-- card !-->
                        </a>
                    </article>
                </div><!-- col !-->
            <?php endwhile;?>
           
        </div><!-- Row !-->
        <?php
        // Pagination
        $big = 999999999; // need an unlikely integer
        echo '<div class="pagination mb-md-5 mb-3">';
        echo paginate_links(array(
            'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
            'format' => '?paged=%#%',
            'current' => max(1, $paged),
            'total' => $news_query->max_num_pages,
        ));
        echo '</div>';
         wp_reset_postdata();
         else :
        echo 'No news found.';
        echo '</div>';
    endif;

    return ob_get_clean();
}

add_shortcode('all-news', 'allNews_shortcode');
