<?php
if (!defined('ABSPATH')) { exit; }
get_header();
?>

<?php if (have_posts()): while (have_posts()): the_post(); ?>
    <?php
    $post_id = get_the_ID();
    $post_categories = get_the_category();
    $primary_category = !empty($post_categories) ? $post_categories[0] : null;
    $clean_content = wp_strip_all_tags((string) get_post_field('post_content', $post_id));
    $word_count = str_word_count($clean_content);
    $reading_minutes = max(1, (int) ceil($word_count / 220));
    $excerpt = get_the_excerpt();

    $related_args = [
        'post_type' => 'post',
        'posts_per_page' => 4,
        'post__not_in' => [$post_id],
        'ignore_sticky_posts' => true,
    ];
    if ($primary_category instanceof WP_Term) {
        $related_args['cat'] = (int) $primary_category->term_id;
    }
    $related_query = new WP_Query($related_args);

    $latest_query = new WP_Query([
        'post_type' => 'post',
        'posts_per_page' => 5,
        'post__not_in' => [$post_id],
        'ignore_sticky_posts' => true,
    ]);
    ?>
    <div class="bp-single-layout">
        <article class="bp-single-article">
            <header class="bp-single-head">
                <?php if (!empty($post_categories)): ?>
                    <div class="bp-single-cats">
                        <?php foreach ($post_categories as $cat): ?>
                            <a href="<?php echo esc_url(get_category_link($cat)); ?>"><?php echo esc_html($cat->name); ?></a>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>

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

                <div class="bp-single-meta-row">
                    <span><?php echo esc_html(get_the_date('Y-m-d')); ?></span>
                    <span><?php echo esc_html(get_the_author()); ?></span>
                    <span><?php echo esc_html($reading_minutes . ' min czytania'); ?></span>
                </div>

                <?php if (!empty($excerpt)): ?>
                    <p class="bp-single-lead"><?php echo esc_html($excerpt); ?></p>
                <?php endif; ?>
            </header>

            <?php if (has_post_thumbnail()): ?>
                <figure class="bp-single-hero">
                    <?php the_post_thumbnail('full', ['class' => 'bp-single-hero-image']); ?>
                    <?php if ($primary_category instanceof WP_Term): ?>
                        <span class="bp-mini-label"><?php echo esc_html($primary_category->name); ?></span>
                    <?php endif; ?>
                </figure>
            <?php endif; ?>

            <div class="bp-single-content">
                <?php the_content(); ?>
            </div>
        </article>

        <aside class="bp-single-sidebar" aria-label="Dodatkowe sekcje artykulu">
            <section class="bp-side-box">
                <h3>Powiazane wpisy</h3>
                <?php if ($related_query->have_posts()): ?>
                    <ul class="bp-side-list">
                        <?php while ($related_query->have_posts()): $related_query->the_post(); ?>
                            <li class="bp-side-item">
                                <a class="bp-side-thumb" href="<?php the_permalink(); ?>" aria-label="<?php the_title_attribute(); ?>">
                                    <?php if (has_post_thumbnail()): ?>
                                        <?php the_post_thumbnail('thumbnail', ['class' => 'bp-side-thumb-image']); ?>
                                    <?php else: ?>
                                        <span class="bp-side-thumb-image bp-mini-placeholder"></span>
                                    <?php endif; ?>
                                </a>
                                <div class="bp-side-copy">
                                    <a class="bp-side-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    <div class="bp-side-date"><?php echo esc_html(get_the_date('Y-m-d')); ?></div>
                                </div>
                            </li>
                        <?php endwhile; ?>
                    </ul>
                <?php else: ?>
                    <p class="bp-side-empty">Brak powiazanych wpisow.</p>
                <?php endif; ?>
                <?php wp_reset_postdata(); ?>
            </section>

            <section class="bp-side-box">
                <h3>Najnowsze wpisy</h3>
                <?php if ($latest_query->have_posts()): ?>
                    <ul class="bp-side-list">
                        <?php while ($latest_query->have_posts()): $latest_query->the_post(); ?>
                            <li class="bp-side-item">
                                <a class="bp-side-thumb" href="<?php the_permalink(); ?>" aria-label="<?php the_title_attribute(); ?>">
                                    <?php if (has_post_thumbnail()): ?>
                                        <?php the_post_thumbnail('thumbnail', ['class' => 'bp-side-thumb-image']); ?>
                                    <?php else: ?>
                                        <span class="bp-side-thumb-image bp-mini-placeholder"></span>
                                    <?php endif; ?>
                                </a>
                                <div class="bp-side-copy">
                                    <a class="bp-side-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    <div class="bp-side-date"><?php echo esc_html(get_the_date('Y-m-d')); ?></div>
                                </div>
                            </li>
                        <?php endwhile; ?>
                    </ul>
                <?php else: ?>
                    <p class="bp-side-empty">Brak wpisow.</p>
                <?php endif; ?>
                <?php wp_reset_postdata(); ?>
            </section>
        </aside>
    </div>
<?php endwhile; endif; ?>

<?php get_footer(); ?>
