<?php
/**
 * Blog Archive Template
 * This template is used for the blog page and category/tag archives
 */

get_header(); ?>

<!-- Blog Hero -->
<section class="blog-hero">
    <div class="container">
        <h1 class="hero-title">
            <?php if (is_home()) : ?>
                SEO & Digital Marketing Blog
            <?php elseif (is_category()) : ?>
                Category: <?php single_cat_title(); ?>
            <?php elseif (is_tag()) : ?>
                Tag: <?php single_tag_title(); ?>
            <?php elseif (is_author()) : ?>
                Author: <?php the_author(); ?>
            <?php elseif (is_date()) : ?>
                Archive: <?php the_archive_title(); ?>
            <?php else : ?>
                <?php the_archive_title(); ?>
            <?php endif; ?>
        </h1>
        <p class="hero-subtitle">
            <?php if (is_home()) : ?>
                Expert tips, industry insights, and proven strategies to help your UK business grow online.
            <?php elseif (is_category()) : ?>
                <?php echo category_description(); ?>
            <?php elseif (is_tag()) : ?>
                <?php echo tag_description(); ?>
            <?php else : ?>
                Browse our articles and insights.
            <?php endif; ?>
        </p>
    </div>
</section>

<!-- Blog Content -->
<section class="section">
    <div class="container">
        <div class="blog-layout">
            <!-- Main Content -->
            <div class="blog-main">
                <?php if (have_posts()) : ?>
                    <div class="blog-posts">
                        <?php while (have_posts()) : the_post(); ?>
                            <article class="blog-post-card pixel-card pixel-shadow">
                                <?php if (has_post_thumbnail()) : ?>
                                    <div class="post-thumbnail">
                                        <a href="<?php the_permalink(); ?>">
                                            <?php the_post_thumbnail('large'); ?>
                                        </a>
                                    </div>
                                <?php endif; ?>
                                
                                <div class="post-content">
                                    <div class="post-meta">
                                        <span class="post-date">
                                            <time datetime="<?php echo get_the_date('c'); ?>">
                                                <?php echo get_the_date(); ?>
                                            </time>
                                        </span>
                                        <span class="post-category">
                                            <?php 
                                            $categories = get_the_category();
                                            if (!empty($categories)) {
                                                echo '<a href="' . esc_url(get_category_link($categories[0]->term_id)) . '">' . esc_html($categories[0]->name) . '</a>';
                                            }
                                            ?>
                                        </span>
                                        <span class="read-time">
                                            <?php echo revd_reading_time(); ?> min read
                                        </span>
                                    </div>
                                    
                                    <h2 class="post-title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h2>
                                    
                                    <div class="post-excerpt">
                                        <?php 
                                        if (has_excerpt()) {
                                            the_excerpt();
                                        } else {
                                            echo wp_trim_words(get_the_content(), 30, '...');
                                        }
                                        ?>
                                    </div>
                                    
                                    <div class="post-footer">
                                        <a href="<?php the_permalink(); ?>" class="read-more pixel-button">
                                            Read Full Article
                                        </a>
                                        <div class="post-tags">
                                            <?php 
                                            $tags = get_the_tags();
                                            if ($tags) {
                                                foreach ($tags as $tag) {
                                                    echo '<span class="tag">' . $tag->name . '</span>';
                                                }
                                            }
                                            ?>
                                        </div>
                                    </div>
                                </div>
                            </article>
                        <?php endwhile; ?>
                    </div>

                    <!-- Pagination -->
                    <div class="blog-pagination">
                        <?php
                        $pagination = paginate_links(array(
                            'prev_text' => '← Previous',
                            'next_text' => 'Next →',
                            'type' => 'array'
                        ));
                        
                        if ($pagination) {
                            echo '<div class="pagination-links">';
                            foreach ($pagination as $link) {
                                echo '<span class="pagination-link">' . $link . '</span>';
                            }
                            echo '</div>';
                        }
                        ?>
                    </div>

                <?php else : ?>
                    <div class="no-posts pixel-card">
                        <h2>No Posts Found</h2>
                        <p>Sorry, but there are no posts to display. Please check back later.</p>
                    </div>
                <?php endif; ?>
            </div>

            <!-- Sidebar - Only on blog pages -->
            <aside class="blog-sidebar">
                <?php get_template_part('template-parts/blog/sidebar'); ?>
            </aside>
        </div>
    </div>
</section>

<?php get_footer(); ?>