<?php
/**
 * Template Name: Blog Custom Layout
 * Template Post Type: page
 *
 * A flexible page template for displaying blog posts in three distinct styles:
 *   - Default: Standard list layout (matching the main blog index).
 *   - Classic: Multi-column card grid layout.
 *   - Modern: Modern card grid layout.
 *
 * Per-page settings (Sidebar, Style, Columns, etc.) are configured via
 * the block editor sidebar panel registered in inc/template-meta.php.
 *
 * @package Blog_Over
 */

get_header();

// — Page Settings ——————————————————————————————————————————————————————————
$page_id = get_the_ID();

$sidebar_position = get_post_meta($page_id, '_blog_over_template_sidebar', true) ?: 'right-sidebar';
$blog_style = get_post_meta($page_id, '_blog_over_template_blog_style', true) ?: 'default';
$grid_columns = (int) (get_post_meta($page_id, '_blog_over_template_grid_columns', true) ?: 3);
$posts_per_page = (int) (get_post_meta($page_id, '_blog_over_template_posts_per_page', true) ?: 9);
$category_filter = get_post_meta($page_id, '_blog_over_template_categories', true) ?: '';
$order_by = get_post_meta($page_id, '_blog_over_template_orderby', true) ?: 'date';
$order = get_post_meta($page_id, '_blog_over_template_order', true) ?: 'DESC';
$show_pagination = get_post_meta($page_id, '_blog_over_template_show_pagination', true) !== '0';
$section_width = get_post_meta($page_id, '_blog_over_template_section_width', true) ?: 'boxed';

// — Layout Flags ——————————————————————————————————————————————————————————
$has_sidebar = ($sidebar_position !== 'no-sidebar');
$sidebar_pos = ($sidebar_position === 'left-sidebar') ? 'left' : 'right';
$is_list_style = ($blog_style === 'list');
$is_modern_style = ($blog_style === 'modern-cards');
$is_classic_style = (!$is_list_style && !$is_modern_style); // value: 'default'

$container_class = ($section_width === 'full-width')
    ? 'blog-over-container blog-over-container-wide'
    : 'blog-over-container';

// — Query ——————————————————————————————————————————————————————————————————
$paged = max(1, (int) get_query_var('paged'));
$args = array(
    'post_type' => 'post',
    'posts_per_page' => $posts_per_page,
    'paged' => $paged,
    'orderby' => sanitize_key($order_by),
    'order' => in_array($order, array('ASC', 'DESC'), true) ? $order : 'DESC',
);

if (!empty($category_filter)) {
    $args['category__in'] = array_map('intval', explode(',', $category_filter));
}

$blog_query = new WP_Query($args);
?>

<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_pos : 'no-sidebar'); ?>">

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

            <div class="blog-over-main-content">
                <?php if ($blog_query->have_posts()): ?>

                    <?php if ($is_list_style): ?>
                        <!-- ── Default / List Style (matches main blog index) ── -->
                        <div class="blog-over-posts-list list-view">
                            <?php
                            while ($blog_query->have_posts()):
                                $blog_query->the_post();
                                get_template_part('template-parts/content');
                            endwhile;
                            ?>
                        </div>

                    <?php elseif ($is_modern_style): ?>
                        <!-- ── Modern Cards Style ── -->
                        <?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-' . $grid_columns . $masonry_class ); ?>">
                            <?php
                            while ($blog_query->have_posts()):
                                $blog_query->the_post();
                                get_template_part('template-parts/content', 'modern');
                            endwhile;
                            ?>
                        </div>

                    <?php else: ?>
                        <!-- ── Classic Grid Style ── -->
                        <div class="blog-over-posts-grid blog-over-grid-cols-<?php echo esc_attr($grid_columns); ?>">
                            <?php
                            while ($blog_query->have_posts()):
                                $blog_query->the_post();
                                get_template_part('template-parts/content', 'grid');
                            endwhile;
                            ?>
                        </div>

                    <?php endif; ?>

                    <?php if ($show_pagination): ?>
                        <nav class="blog-over-pagination"
                            aria-label="<?php esc_attr_e('Posts Pagination', 'blog-over'); ?>">
                            <?php
                            echo paginate_links(array(
                                'total' => $blog_query->max_num_pages,
                                'current' => $paged,
                                '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; ?>

                <?php wp_reset_postdata(); ?>
            </div><!-- .blog-over-main-content -->

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

        </div><!-- .blog-over-content-wrapper -->
    </div><!-- .container -->
</main>

<?php get_footer();
