<?php

get_header(); ?>

<main id="main-content">
    <section class="search-header">
        <div class="landing-container">
            <h1>
                <?php
               
                printf(esc_html__('Search Results for: %s', 'theme-retry'), 
                    '<span class="search-query">' . get_search_query() . '</span>'
                );
                ?>
            </h1>

            <?php get_search_form(); ?>
        </div>
    </section>

    <?php if (have_posts()) : ?>
        <div class="foodcards-container">
            <?php
            while (have_posts()) :
                the_post();
                ?>
                <article id="post-<?php the_ID(); ?>" <?php post_class('foodcard'); ?>>
                    <?php if (has_post_thumbnail()) : ?>
                        <div class="post-thumbnail">
                            <?php the_post_thumbnail('medium', array(
                                'alt' => get_the_title(),
                                'width' => '200',
                                'height' => '200'
                            )); ?>
                        </div>
                    <?php endif; ?>

                    <header class="entry-header">
                        <?php the_title('<h2 class="entry-title"><a href="' . esc_url(get_permalink()) . '">', '</a></h2>'); ?>
                    </header>

                    <div class="entry-summary">
                        <?php the_excerpt(); ?>
                    </div>

                    <footer class="entry-footer">
                        <div class="entry-meta">
                            <?php
                            
                            $post_type = get_post_type_object(get_post_type());
                            if ($post_type) {
                                echo '<span class="post-type">' . esc_html($post_type->labels->singular_name) . '</span>';
                            }

                            
                            $categories_list = get_the_category_list(', ');
                            if ($categories_list) {
                                echo '<span class="cat-links">' . $categories_list . '</span>';
                            }
                            ?>
                        </div>
                        <a href="<?php echo esc_url(get_permalink()); ?>" class="btn">
                            Read More<span class="sr-only"> about <?php echo get_the_title(); ?></span>
                        </a>
                    </footer>
                </article>
            <?php endwhile; ?>
        </div>

        <?php
        the_posts_pagination(array(
            'prev_text' => __('Previous page', 'theme-retry'),
            'next_text' => __('Next page', 'theme-retry'),
            'before_page_number' => '<span class="meta-nav screen-reader-text">' . __('Page', 'theme-retry') . ' </span>',
        ));
        ?>

    <?php else : ?>
        <div class="no-results">
            <h2><?php esc_html_e('Nothing Found', 'theme-retry'); ?></h2>
            <p><?php esc_html_e('Sorry, but nothing matched your search terms. Please try again with some different keywords.', 'theme-retry'); ?></p>
            
            <div class="search-suggestions">
                <h3><?php esc_html_e('Search Suggestions:', 'theme-retry'); ?></h3>
            </div>

            <?php get_search_form(); ?>
        </div>
    <?php endif; ?>
</main>

<?php get_footer(); ?>
