<?php
/**
 * Custom archive template (Theme Engine) — used for:
 *  - is_archive() : categories, tags, authors, date archives
 *  - is_home()    : blog index (if show_on_front=posts)
 *  - is_search()  : search results
 *
 * The inner loop emits conventional class hooks only (.posts-grid, .post-card)
 * so the pushed global CSS owns the visual presentation.
 */
if ( ! defined( 'ABSPATH' ) ) exit;

$lq_engine = new Linkquiver_Theme_Engine();
$lq_header = do_shortcode( $lq_engine->get_header_html() );
$lq_footer = do_shortcode( $lq_engine->get_footer_html() );

if ( is_search() ) {
    $lq_title = sprintf( __( 'Results for “%s”', 'linkquiver' ), esc_html( get_search_query() ) );
    $lq_desc  = '';
} elseif ( is_category() || is_tag() || is_tax() ) {
    $lq_title = single_term_title( '', false );
    $lq_desc  = term_description();
} elseif ( is_author() ) {
    $lq_title = get_the_author();
    $lq_desc  = get_the_author_meta( 'description' );
} elseif ( is_post_type_archive() ) {
    $lq_title = post_type_archive_title( '', false );
    $lq_desc  = '';
} else {
    $lq_title = __( 'Our articles', 'linkquiver' );
    $lq_desc  = '';
}
?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<?php wp_body_open(); ?>
<?php echo $lq_header; ?>

<main class="site-main">
    <header class="archive-header">
        <div class="container">
            <span class="archive-eyebrow"><?php
                if ( is_category() ) esc_html_e( 'Category', 'linkquiver' );
                elseif ( is_tag() ) esc_html_e( 'Tag', 'linkquiver' );
                elseif ( is_author() ) esc_html_e( 'Author', 'linkquiver' );
                elseif ( is_search() ) esc_html_e( 'Search', 'linkquiver' );
                else esc_html_e( 'Archive', 'linkquiver' );
            ?></span>
            <h1 class="archive-title"><?php echo esc_html( $lq_title ); ?></h1>
            <?php if ( $lq_desc ): ?>
                <div class="archive-description"><?php echo wp_kses_post( $lq_desc ); ?></div>
            <?php endif; ?>
        </div>
    </header>

    <div class="container">
        <?php if ( have_posts() ): ?>
            <div class="posts-grid">
                <?php while ( have_posts() ): the_post(); ?>
                    <article class="post-card">
                        <a class="post-card-link" href="<?php the_permalink(); ?>">
                            <?php if ( has_post_thumbnail() ): ?>
                                <div class="post-card-media">
                                    <?php the_post_thumbnail( 'large', array( 'loading' => 'lazy' ) ); ?>
                                    <?php
                                        $lq_cats = get_the_category();
                                        if ( ! empty( $lq_cats ) ):
                                    ?>
                                        <span class="post-card-category"><?php echo esc_html( $lq_cats[0]->name ); ?></span>
                                    <?php endif; ?>
                                </div>
                            <?php endif; ?>
                            <div class="post-card-body">
                                <h2 class="post-card-title"><?php the_title(); ?></h2>
                                <time class="post-card-date" datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>"><?php echo esc_html( get_the_date() ); ?></time>
                                <p class="post-card-excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 26, '…' ) ); ?></p>
                            </div>
                        </a>
                    </article>
                <?php endwhile; ?>
            </div>

            <nav class="pagination" aria-label="Pagination">
                <?php echo paginate_links( array(
                    'prev_text' => '‹',
                    'next_text' => '›',
                    'mid_size'  => 1,
                ) ); ?>
            </nav>
        <?php else: ?>
            <div class="posts-empty">
                <p><?php esc_html_e( 'No posts yet. Check back soon.', 'linkquiver' ); ?></p>
            </div>
        <?php endif; ?>
    </div>
</main>

<?php echo $lq_footer; ?>
<?php wp_footer(); ?>
</body>
</html>
