<?php
/**
 * Template part for displaying single post content
 *
 * @package Blog_Over
 */

// Get post elements settings
$post_elements = get_theme_mod('blog_over_single_elements', array('category', 'meta', 'tags', 'share', 'author_box', 'post_nav', 'comments'));
$meta_elements = get_theme_mod('blog_over_single_meta_elements', array('author', 'date', 'comments'));
$show_featured = get_theme_mod('blog_over_single_show_featured', true);

// Note: blog_over_render_single_element() and blog_over_render_single_meta_item() 
// are now defined in inc/template-functions.php to prevent redeclaration errors
?>

<article id="post-<?php the_ID(); ?>" <?php post_class('blog-over-single-post'); ?>>

    <div class="blog-over-single-content-box blog-over-box-style">
        <header class="blog-over-single-header">
            <?php
            // Check title position setting
            $title_position = get_theme_mod('blog_over_single_title_position', 'in-page-header');

            // Render category (before title if in content)
            if ($title_position === 'in-content' && in_array('category', $post_elements)) {
                blog_over_render_single_element('category', $meta_elements);
            }

            // Render title if position is in-content
            if ($title_position === 'in-content') {
                ?>
                <h1 class="blog-over-single-title"><?php the_title(); ?></h1>
                <?php
            }

            // Render meta ONLY when title position is in-content (avoid duplicate with page header)
            if ($title_position === 'in-content' && in_array('meta', $post_elements)) {
                blog_over_render_single_element('meta', $meta_elements);
            }
            ?>
        </header>

        <?php
        // Featured Image
        global $blog_over_featured_used_as_header_bg;
        if ($show_featured && has_post_thumbnail() && empty($blog_over_featured_used_as_header_bg)):
            ?>
            <div class="blog-over-single-thumbnail">
                <?php the_post_thumbnail('blog-over-featured'); ?>
            </div>
        <?php endif; ?>

        <div class="blog-over-single-content">
            <?php
            if (get_theme_mod('blog_over_single_drop_caps', false)) {
                echo '<div class="has-drop-cap">';
            }

            the_content();

            if (get_theme_mod('blog_over_single_drop_caps', false)) {
                echo '</div>';
            }

            wp_link_pages(array(
                'before' => '<div class="blog-over-page-links">' . esc_html__('Pages:', 'blog-over'),
                'after' => '</div>',
            ));
            ?>
        </div>

        <?php
        // Render Tags and Share BUTTONS inside the main content box
        foreach ($post_elements as $element) {
            if (in_array($element, array('tags', 'share'))) {
                blog_over_render_single_element($element, $meta_elements);
            }
        }
        ?>
    </div><!-- .blog-over-single-content-box -->

    <?php
    // Render Author Box and Post Nav OUTSIDE the main content box (Separate Cards)
    foreach ($post_elements as $element) {
        if (in_array($element, array('author_box', 'post_nav'))) {
            blog_over_render_single_element($element, $meta_elements);
        }
    }
    ?>
</article>

<?php
// Related Posts (controlled from Related Posts section in Kirki)
if (get_theme_mod('blog_over_enable_related', true)) {
    blog_over_related_posts();
}

// Comments (render from sortable elements)
foreach ($post_elements as $element) {
    if ($element === 'comments') {
        blog_over_render_single_element($element, $meta_elements);
        break;
    }
}
?>