<?php
/**
 * The main template file
 *
 * @package Blog_Over
 */

get_header();

$layout = get_theme_mod('Blog_Over_layout', 'right-sidebar');
$blog_style = get_theme_mod('Blog_Over_style', 'default');
$is_modern    = ($blog_style === 'modern-cards');
$is_editorial = ($blog_style === 'editorial');

// Section Width setting (Blog only)
$section_width = get_theme_mod('Blog_Over_section_width', 'boxed');
$container_class = ($section_width === 'full-width') ? 'blog-over-container blog-over-container-wide' : 'blog-over-container';

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

// Modern Card Grid columns: 2 with sidebar, 3 without sidebar
$modern_cols = $has_sidebar ? 2 : 3;
?>

<main id="primary" class="blog-over-site-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="<?php echo esc_attr($container_class); ?>">
        <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 if (have_posts()): ?>
                    <?php if ($is_modern): ?>
                        <!-- Modern Card Grid Layout -->
                        <?php $masonry_class = (get_theme_mod('blog_over_grid_layout_mode', 'default') === 'masonry') ? ' bo-masonry' : ''; ?>
                        <div class="<?php echo esc_attr( 'blog-over-modern-grid blog-over-grid-cols-' . $modern_cols . $masonry_class ); ?>">
                            <?php
                            while (have_posts()):
                                the_post();
                                get_template_part('template-parts/content', 'modern');
                            endwhile;
                            ?>
                        </div>
                    <?php elseif ($is_editorial): ?>
                        <!-- Editorial List Layout -->
                        <div class="blog-over-editorial-list">
                        <?php
                        $editorial_idx = 0;
                        while (have_posts()):
                            the_post();
                            set_query_var('blog_over_editorial_index', $editorial_idx);
                            // ALL three modes: all posts = full-width split cards
                            // Z-Pattern  → index 0=left, 1=right, 2=left ...
                            // Left Image → all cards image on left
                            // Right Image → all cards image on right
                            get_template_part('template-parts/content', 'editorial');
                            $editorial_idx++;
                        endwhile;
                        ?>
                        </div>
                    <?php else: ?>
                        <!-- Default Layout -->
                        <div class="blog-over-posts-list <?php echo esc_attr($is_grid ? 'grid-view' : 'list-view'); ?>">
                            <?php
                            while (have_posts()):
                                the_post();
                                get_template_part('template-parts/content');
                            endwhile;
                            ?>
                        </div>
                    <?php endif; ?>

                    <?php if (get_theme_mod('blog_over_show_pagination', true)): ?>
                        <?php 
                        $pag_style = sanitize_key(get_theme_mod('blog_over_pagination_style', 'modern')); 
                        
                        $pagination_html = '<nav class="blog-over-pagination blog-over-pagination-' . esc_attr($pag_style) . '">';
                        $pagination_html .= get_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>',
                        ));
                        $pagination_html .= '</nav>';

                        // Allow plugins (like Blog Over Pro) to override pagination with infinite scroll / load more
                        echo apply_filters('blog_over_pagination_output', $pagination_html);
                        ?>
                    <?php endif; ?>
                <?php else: ?>
                    <div class="blog-over-no-posts">
                        <p><?php esc_html_e('No posts found.', '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();

