<?php
/**
 * The template for displaying search results
 *
 * @package Blog_Over
 */

get_header();

$layout = get_theme_mod('Blog_Over_layout', 'right-sidebar');

// Parse layout values from Kirki (right-sidebar, left-sidebar, full-width)
$has_sidebar = ($layout !== 'full-width');
$sidebar_position = ($layout === 'left-sidebar') ? 'left' : 'right';
$is_grid = (strpos($layout, 'grid') !== false);
?>

<main id="primary" class="blog-over-site-main blog-over-search-main">
    <div class="blog-over-page-header-container">
        <?php blog_over_page_header(); ?>
    </div>
    <?php if (blog_over_should_render_breadcrumb_container()): ?>
        <div class="blog-over-breadcrumb-container">
            <?php blog_over_breadcrumb(); ?>
        </div>
    <?php endif; ?>

    <div class="blog-over-container">
        <div
            class="blog-over-content-wrapper <?php echo esc_attr($has_sidebar ? 'has-sidebar sidebar-' . $sidebar_position : 'no-sidebar'); ?>">

            <?php if ($has_sidebar && $sidebar_position === 'left'): ?>
                <aside class="blog-over-sidebar">
                    <?php get_sidebar(); ?>
                </aside>
            <?php endif; ?>

            <div class="blog-over-main-content">
                <?php
                /**
                 * Search Header Rule: 
                 * If Page Header is enabled, we hide the local search title to avoid duplication.
                 */
                if (!blog_over_has_page_header() || !get_theme_mod('blog_over_page_header_show_title', true)):
                    ?>
                    <header class="blog-over-search-header">
                        <h1 class="blog-over-search-title">
                            <?php
                            printf(
                                esc_html__('Search Results for: %s', 'blog-over'),
                                '<span>' . get_search_query() . '</span>'
                            );
                            ?>
                        </h1>
                    </header>
                <?php endif; ?>

                <div class="blog-over-search-form-wrapper">
                    <?php get_search_form(); ?>
                </div>

                <?php if (have_posts()): ?>
                    <div class="blog-over-posts-list <?php echo esc_attr($is_grid ? 'grid-view' : 'list-view'); ?>">
                        <?php
                        while (have_posts()):
                            the_post();
                            if ($is_grid) {
                                get_template_part('template-parts/content', 'grid');
                            } else {
                                get_template_part('template-parts/content');
                            }
                        endwhile;
                        ?>
                    </div>

                    <?php if (get_theme_mod('blog_over_show_pagination', true)): ?>
                        <nav class="blog-over-pagination">
                            <?php
                            the_posts_pagination(array(
                                'mid_size' => 2,
                                'prev_text' => '<i class="fas fa-chevron-left"></i>',
                                'next_text' => '<i class="fas fa-chevron-right"></i>',
                            ));
                            ?>
                        </nav>
                    <?php endif; ?>
                <?php else: ?>
                    <div class="blog-over-no-results">
                        <h2><?php esc_html_e('Nothing Found', 'blog-over'); ?></h2>
                        <p><?php esc_html_e('Sorry, no results were found for your search. Please try again with different keywords.', 'blog-over'); ?>
                        </p>
                    </div>
                <?php endif; ?>
            </div>

            <?php if ($has_sidebar && $sidebar_position === 'right'): ?>
                <aside class="blog-over-sidebar">
                    <?php get_sidebar(); ?>
                </aside>
            <?php endif; ?>

        </div>
    </div>
</main>

<?php
get_footer();
