<?php get_header(); ?>

<?php
// Sidebar-Einstellungen aus dem Customizer
$sidebar_enabled  = get_theme_mod( 'single_sidebar_enabled', true );
$sidebar_position = get_theme_mod( 'single_sidebar_position', 'right' );
$has_sidebar      = $sidebar_enabled && is_active_sidebar( 'single-post-sidebar' );

$layout_class = $has_sidebar
    ? 'single-layout with-sidebar sidebar-' . esc_attr( $sidebar_position )
    : 'single-layout';
?>

<main id="primary" class="site-main">
    <div class="container">
        <div class="<?php echo esc_attr( $layout_class ); ?>">

            <?php if ( $has_sidebar && $sidebar_position === 'left' ) : ?>
                <aside class="single-sidebar">
                    <?php minecraft_modern_render_single_sidebar(); ?>
                </aside>
            <?php endif; ?>

            <div class="single-main-content">
                <?php while ( have_posts() ) : the_post(); ?>

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

                        <?php if ( has_post_thumbnail() ) : ?>
                            <div class="post-hero-image">
                                <?php the_post_thumbnail( 'large' ); ?>
                            </div>
                        <?php endif; ?>

                        <div class="post-content">

                            <?php if ( get_theme_mod( 'single_show_breadcrumb', true ) ) : ?>
                        <nav class="breadcrumb" aria-label="Breadcrumb">
                                <a href="<?php echo esc_url( home_url('/') ); ?>"><?php _e('Startseite', 'minecraft-modern-theme'); ?></a>
                                <span class="breadcrumb-sep"><i class="fas fa-chevron-right"></i></span>
                                <?php
                                $cats = get_the_category();
                                if ( $cats ) :
                                    $cat = $cats[0];
                                ?>
                                    <a href="<?php echo esc_url( get_category_link( $cat->term_id ) ); ?>"><?php echo esc_html( $cat->name ); ?></a>
                                    <span class="breadcrumb-sep"><i class="fas fa-chevron-right"></i></span>
                                <?php endif; ?>
                                <span class="breadcrumb-current"><?php the_title(); ?></span>
                            </nav>
                        <?php endif; ?>

                            <h1 class="post-title"><?php the_title(); ?></h1>

                            <div class="post-meta">
                                <span class="post-meta-item post-author">
                                    <?php echo get_avatar( get_the_author_meta('ID'), 24, '', '', array('class' => 'author-avatar') ); ?>
                                    <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta('ID') ) ); ?>"><?php the_author(); ?></a>
                                </span>
                                <span class="post-meta-item post-date">
                                    <i class="fas fa-calendar-alt"></i>
                                    <time datetime="<?php echo esc_attr( get_the_date('c') ); ?>"><?php echo esc_html( get_the_date() ); ?></time>
                                </span>
                                <?php if ( get_the_modified_date() !== get_the_date() ) : ?>
                                    <span class="post-meta-item post-updated">
                                        <i class="fas fa-pencil-alt"></i>
                                        <?php printf( __('Aktualisiert: %s', 'minecraft-modern-theme'), '<time datetime="' . esc_attr( get_the_modified_date('c') ) . '">' . esc_html( get_the_modified_date() ) . '</time>' ); ?>
                                    </span>
                                <?php endif; ?>
                                <?php
                                $content    = get_post_field( 'post_content', get_the_ID() );
                                $word_count = str_word_count( strip_tags( $content ) );
                                $read_time  = max( 1, ceil( $word_count / 200 ) );
                                ?>
                                <span class="post-meta-item post-read-time">
                                    <i class="fas fa-clock"></i>
                                    <?php printf( _n('%d Min. Lesezeit', '%d Min. Lesezeit', $read_time, 'minecraft-modern-theme'), $read_time ); ?>
                                </span>
                                <?php if ( comments_open() ) : ?>
                                    <span class="post-meta-item post-comments">
                                        <i class="fas fa-comment"></i>
                                        <a href="#comments"><?php comments_number( __('0 Kommentare', 'minecraft-modern-theme'), __('1 Kommentar', 'minecraft-modern-theme'), __('% Kommentare', 'minecraft-modern-theme') ); ?></a>
                                    </span>
                                <?php endif; ?>
                            </div>

                            <?php $cats = get_the_category(); if ( $cats ) : ?>
                                <div class="post-categories-top">
                                    <?php foreach ( $cats as $cat ) : ?>
                                        <a href="<?php echo esc_url( get_category_link( $cat->term_id ) ); ?>" class="post-category-badge"><?php echo esc_html( $cat->name ); ?></a>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>

                            <div class="post-entry">
                                <?php the_content(); ?>
                            </div>

                            <footer class="post-footer">
                                <?php $tags = get_the_tags(); if ( $tags ) : ?>
                                    <div class="post-tags">
                                        <i class="fas fa-tags"></i>
                                        <?php foreach ( $tags as $tag ) : ?>
                                            <a href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" class="post-tag">#<?php echo esc_html( $tag->name ); ?></a>
                                        <?php endforeach; ?>
                                    </div>
                                <?php endif; ?>

                                <nav class="post-navigation" aria-label="<?php esc_attr_e('Beitrag Navigation', 'minecraft-modern-theme'); ?>">
                                    <?php $prev_post = get_previous_post(); $next_post = get_next_post(); ?>
                                    <?php if ( $prev_post ) : ?>
                                        <a href="<?php echo esc_url( get_permalink( $prev_post->ID ) ); ?>" class="post-nav-link post-nav-prev">
                                            <span class="post-nav-label"><i class="fas fa-arrow-left"></i> <?php _e('Vorheriger Beitrag', 'minecraft-modern-theme'); ?></span>
                                            <span class="post-nav-title"><?php echo esc_html( get_the_title( $prev_post->ID ) ); ?></span>
                                        </a>
                                    <?php endif; ?>
                                    <?php if ( $next_post ) : ?>
                                        <a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>" class="post-nav-link post-nav-next">
                                            <span class="post-nav-label"><?php _e('Nächster Beitrag', 'minecraft-modern-theme'); ?> <i class="fas fa-arrow-right"></i></span>
                                            <span class="post-nav-title"><?php echo esc_html( get_the_title( $next_post->ID ) ); ?></span>
                                        </a>
                                    <?php endif; ?>
                                </nav>
                            </footer>

                        </div><!-- .post-content -->
                    </article>

                    <!-- Related Posts -->
                    <?php if ( get_theme_mod( 'single_show_related_posts', true ) ) : ?>
                    <?php
                    $current_cats = wp_get_post_categories( get_the_ID() );
                    if ( $current_cats ) :
                        $related = new WP_Query( array(
                            'category__in'   => $current_cats,
                            'post__not_in'   => array( get_the_ID() ),
                            'posts_per_page' => 3,
                            'orderby'        => 'rand',
                        ) );
                        if ( $related->have_posts() ) : ?>
                            <section class="related-posts">
                                <h3 class="related-posts-title">
                                    <i class="fas fa-layer-group"></i> <?php _e('Ähnliche Beiträge', 'minecraft-modern-theme'); ?>
                                </h3>
                                <div class="related-posts-grid">
                                    <?php while ( $related->have_posts() ) : $related->the_post(); ?>
                                        <article class="related-post-card">
                                            <?php if ( has_post_thumbnail() ) : ?>
                                                <a href="<?php the_permalink(); ?>" class="related-post-thumb">
                                                    <?php the_post_thumbnail('medium', array('loading' => 'lazy')); ?>
                                                </a>
                                            <?php endif; ?>
                                            <div class="related-post-info">
                                                <h4 class="related-post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                                                <span class="related-post-date"><i class="fas fa-calendar-alt"></i> <?php echo esc_html( get_the_date() ); ?></span>
                                            </div>
                                        </article>
                                    <?php endwhile; wp_reset_postdata(); ?>
                                </div>
                            </section>
                        <?php endif;
                    endif; ?>
                    <?php endif; // single_show_related_posts ?>

                    <?php comments_template(); ?>

                <?php endwhile; ?>
            </div><!-- .single-main-content -->

            <?php if ( $has_sidebar && $sidebar_position === 'right' ) : ?>
                <aside class="single-sidebar">
                    <?php minecraft_modern_render_single_sidebar(); ?>
                </aside>
            <?php endif; ?>

        </div><!-- .single-layout -->
    </div>
</main>

<?php get_footer(); ?>