<?php
/**
 * Template Name: Page Layout
 * Template Post Type: page
 *
 * A page template with per-page settings for sidebar position
 * and section width, without blog-related settings.
 *
 * @package Blog_Over
 */

get_header();

// Get page ID
$page_id = get_the_ID();

// Get template settings with defaults
$sidebar_position = get_post_meta($page_id, '_blog_over_template_sidebar', true);
$sidebar_position = $sidebar_position ?: 'right-sidebar';

// Section Width
$section_width = get_post_meta($page_id, '_blog_over_template_section_width', true);
$section_width = $section_width ?: 'boxed';
$container_class = ($section_width === 'full-width') ? 'blog-over-container blog-over-container-wide' : 'blog-over-container';

// Parse layout values
$has_sidebar = ($sidebar_position !== 'no-sidebar');
$sidebar_pos = ($sidebar_position === 'left-sidebar') ? 'left' : 'right';
?>

<main id="primary" class="blog-over-site-main blog-over-page-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 blog-over-page-content-wrapper">
                <?php
                while (have_posts()):
                    the_post();
                    get_template_part('template-parts/content', 'page');
                endwhile;
                ?>
            </div>

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

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

<?php
get_footer();
