<?php
/**
 * The template for displaying archive pages
 *
 * @package Blog_Over
 */

get_header();

/**
 * Archive Override Logic:
 * - If blog_over_archive_override is ON → use archive-specific settings
 * - If blog_over_archive_override is OFF → fallback to blog settings
 */
$archive_override = get_theme_mod('blog_over_archive_override', false);

// Layout
if ($archive_override) {
    $layout = get_theme_mod('blog_over_archive_layout', 'right-sidebar');
} else {
    $layout = get_theme_mod('Blog_Over_layout', 'right-sidebar');
}

// Blog Style
if ($archive_override) {
    $blog_style = get_theme_mod('blog_over_archive_style', 'default');
} else {
    $blog_style = get_theme_mod('Blog_Over_style', 'default');
}
$is_modern    = ($blog_style === 'modern-cards');
$is_editorial = ($blog_style === 'editorial');

// Parse layout values
$has_sidebar = ($layout !== 'full-width');
$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;

// Archive display settings (always archive-specific)
$show_header = get_theme_mod('blog_over_archive_header', true);
$show_description = get_theme_mod('blog_over_archive_description', true);
?>

<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="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 blog-over-archive-content-wrapper">
                <?php
                /**
                 * Archive Header Rule: 
                 * If Page Header is enabled, we hide the local archive header to avoid duplication.
                 * Local header is only used if Page Header is disabled.
                 */
                if (!blog_over_has_page_header() || !get_theme_mod('blog_over_page_header_show_title', true)):
                    ?>
                    <header class="blog-over-archive-header">
                        <?php
                        the_archive_title('<h1 class="blog-over-archive-title">', '</h1>');
                        if ($show_description) {
                            the_archive_description('<div class="blog-over-archive-description">', '</div>');
                        }
                        ?>
                    </header>
                <?php endif; ?>

                <?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;
                        $editorial_has_items = false;
                        while (have_posts()):
                            the_post();
                            set_query_var('blog_over_editorial_index', $editorial_idx);
                            if ($editorial_idx === 0) {
                                get_template_part('template-parts/content', 'editorial');
                            } else {
                                if (!$editorial_has_items) {
                                    echo '<div class="blog-over-editorial-items">';
                                    $editorial_has_items = true;
                                }
                                get_template_part('template-parts/content', 'editorial');
                            }
                            $editorial_idx++;
                        endwhile;
                        if ($editorial_has_items) {
                            echo '</div>';
                        }
                        ?>
                        </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)): ?>
                        <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-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();
