<?php
/**
 * Template for displaying single posts and pages.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 * @package Vintuna
 * @since 1.0.0
 */

get_header();

// Theme defaults
$vintuna_default = vintuna_get_default_theme_options();

// Get global settings
$sidebar  = get_theme_mod('global_sidebar_layout', $vintuna_default['global_sidebar_layout']);
$nav_type = get_theme_mod('wpi_navigation_type', $vintuna_default['wpi_navigation_type']);
$trending_page = get_theme_mod('vintuna_header_trending_page');
$popular_page  = get_theme_mod('vintuna_header_popular_page');

$article_wrap_class = '';
$single_layout_class = 'single-layout-default';

if (have_posts()) :
    while (have_posts()) : the_post();

        $current_id = get_the_ID();

        // Per-post settings
        $post_sidebar = get_post_meta($current_id, 'vintuna_post_sidebar_option', true);
        $post_layout  = get_post_meta($current_id, 'vintuna_post_layout', true);
        $post_nav     = get_post_meta($current_id, 'wpi_disable_ajax_load_next_post', true);

        // Navigation type (fallback to global if needed)
        if ($post_nav === '' || $post_nav === 'global-layout') {
            $post_nav = $nav_type;
        }

        // Sidebar override
        if (!empty($post_sidebar) && $post_sidebar !== 'global-sidebar') {
            $sidebar = esc_attr($post_sidebar);
        }

        // Layout adjustments
        if ($post_layout === 'layout-2') {
            $single_layout_class = ' single-layout-banner';
        }
        if ($current_id == $trending_page || $current_id == $popular_page) {
            $single_layout_class = '';
            $breadcrumb = true;
        }
        ?>

        <div class="theme-block theme-block-single">
            <div id="primary" class="content-area">
                <main id="main" class="site-main" role="main">

                    <?php
                    if (!is_home() && !is_front_page() && (isset($breadcrumb) || $post_layout !== 'layout-2')) {
                        vintuna_breadcrumb_with_title_block();
                    }
                    ?>

                    <div class="article-wraper single-layout <?php echo esc_attr($article_wrap_class . $single_layout_class); ?>">

                        <?php
                        get_template_part('template-parts/content', 'single');

                        // Comments (exclude trending/popular pages)
                        if ($current_id != $trending_page && $current_id != $popular_page) {
                            if ((is_single() || is_page()) && (comments_open() || get_comments_number()) && !post_password_required()) {
                                echo '<div class="comments-wrapper">';
                                comments_template();
                                echo '</div>';
                            }
                        }
                        ?>

                    </div>

                    <?php
                    /**
                     * Navigation hooks
                     * - vintuna_post_floating_nav (10)
                     * - vintuna_related_posts (20)
                     * - vintuna_single_post_navigation (30)
                     */
                    do_action('vintuna_navigation_action');
                    ?>

                </main>
            </div>

            <?php
            // Sidebar (disabled on WooCommerce cart/checkout)
            $sidebar_status = !(class_exists('WooCommerce') && (is_cart() || is_checkout()));
            if ($sidebar !== 'no-sidebar' && $sidebar_status) {
                get_sidebar();
            }
            ?>
        </div>

    <?php
    endwhile;
else :
    get_template_part('template-parts/content', 'none');
endif;

get_footer();
