<?php get_header(); ?>

<main id="primary" class="site-main">
    <div class="container">
        <div class="content-area">

            <header class="archive-header">
                <?php
                if ( is_category() ) {
                    echo '<div class="archive-type-badge"><i class="fas fa-folder-open"></i> ' . __('Kategorie', 'minecraft-modern-theme') . '</div>';
                    the_archive_title( '<h1 class="archive-title">', '</h1>' );
                    the_archive_description( '<div class="archive-description">', '</div>' );
                } elseif ( is_tag() ) {
                    echo '<div class="archive-type-badge"><i class="fas fa-hashtag"></i> ' . __('Tag', 'minecraft-modern-theme') . '</div>';
                    the_archive_title( '<h1 class="archive-title">', '</h1>' );
                    the_archive_description( '<div class="archive-description">', '</div>' );
                } elseif ( is_author() ) {
                    $author = get_queried_object();
                    echo '<div class="archive-type-badge"><i class="fas fa-user"></i> ' . __('Autor', 'minecraft-modern-theme') . '</div>';
                    ?>
                    <div class="archive-author-header">
                        <?php echo get_avatar( $author->ID, 80, '', '', array('class' => 'archive-author-avatar') ); ?>
                        <div>
                            <h1 class="archive-title"><?php echo esc_html( $author->display_name ); ?></h1>
                            <?php if ( $author->description ) : ?>
                                <p class="archive-description"><?php echo esc_html( $author->description ); ?></p>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php
                } elseif ( is_date() ) {
                    echo '<div class="archive-type-badge"><i class="fas fa-calendar"></i> ' . __('Archiv', 'minecraft-modern-theme') . '</div>';
                    the_archive_title( '<h1 class="archive-title">', '</h1>' );
                } else {
                    the_archive_title( '<h1 class="archive-title">', '</h1>' );
                    the_archive_description( '<div class="archive-description">', '</div>' );
                }
                ?>
            </header>

            <?php if ( have_posts() ) : ?>

                <div class="archive-posts-grid">
                    <?php while ( have_posts() ) : the_post(); ?>

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

                            <?php if ( has_post_thumbnail() ) : ?>
                                <div class="archive-card-thumb">
                                    <a href="<?php the_permalink(); ?>">
                                        <?php the_post_thumbnail('medium_large', array('loading' => 'lazy')); ?>
                                    </a>
                                </div>
                            <?php endif; ?>

                            <div class="archive-card-body">

                                <?php $cats = get_the_category(); if ( $cats ) : ?>
                                    <div class="archive-card-cats">
                                        <a href="<?php echo esc_url( get_category_link( $cats[0]->term_id ) ); ?>" class="post-category-badge">
                                            <?php echo esc_html( $cats[0]->name ); ?>
                                        </a>
                                    </div>
                                <?php endif; ?>

                                <h2 class="archive-card-title">
                                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                </h2>

                                <div class="archive-card-meta">
                                    <span><i class="fas fa-calendar-alt"></i> <?php echo esc_html( get_the_date() ); ?></span>
                                    <span><i class="fas fa-user"></i> <?php the_author(); ?></span>
                                    <?php
                                    $wc = str_word_count( strip_tags( get_post_field('post_content', get_the_ID()) ) );
                                    $rt = max(1, ceil($wc / 200));
                                    ?>
                                    <span><i class="fas fa-clock"></i> <?php printf( _n('%d Min.', '%d Min.', $rt, 'minecraft-modern-theme'), $rt ); ?></span>
                                </div>

                                <div class="archive-card-excerpt">
                                    <?php the_excerpt(); ?>
                                </div>

                                <a href="<?php the_permalink(); ?>" class="archive-card-read-more">
                                    <?php _e('Weiterlesen', 'minecraft-modern-theme'); ?> <i class="fas fa-arrow-right"></i>
                                </a>

                            </div>
                        </article>

                    <?php endwhile; ?>
                </div>

                <!-- Pagination -->
                <div class="archive-pagination">
                    <?php
                    the_posts_pagination( array(
                        'mid_size'           => 2,
                        'prev_text'          => '<i class="fas fa-chevron-left"></i> ' . __('Zurück', 'minecraft-modern-theme'),
                        'next_text'          => __('Weiter', 'minecraft-modern-theme') . ' <i class="fas fa-chevron-right"></i>',
                        'before_page_number' => '<span class="page-num">',
                        'after_page_number'  => '</span>',
                    ) );
                    ?>
                </div>

            <?php else : ?>

                <div class="no-posts-found">
                    <i class="fas fa-search fa-2x"></i>
                    <p><?php _e('Keine Beiträge gefunden.', 'minecraft-modern-theme'); ?></p>
                </div>

            <?php endif; ?>

        </div>
    </div>
</main>

<?php get_footer(); ?>