<?php
/**
 * Template part for displaying featured post on blog page
 *
 * @package shopbar
 */

$categories      = get_the_category();
$category        = ! empty( $categories ) ? $categories[0] : null;
$reading_time    = shopbar_estimated_reading_time( get_the_content() );
$blog_page_id    = get_option( 'page_for_posts' );
$blog_home_url   = $blog_page_id ? get_permalink( $blog_page_id ) : home_url( '/?post_type=post' );
?>

<!-- Featured Post (split layout) -->
<article class="blog-featured fade-in">
    <a href="<?php the_permalink(); ?>" class="blog-featured-media" aria-label="<?php echo esc_attr( get_the_title() ); ?>">
        <?php if ( has_post_thumbnail() ) : ?>
            <?php the_post_thumbnail( 'large', array( 'alt' => esc_attr( get_the_title() ) ) ); ?>
        <?php else : ?>
            <img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/placeholder.svg' ); ?>" alt="<?php echo esc_attr( get_the_title() ); ?>" loading="eager">
        <?php endif; ?>
    </a>

    <div class="blog-featured-body">
        <span class="blog-tag"><?php esc_html_e( 'Featured', 'shopbar' ); ?></span>

        <div class="blog-meta-line">
            <time datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>"><?php echo esc_html( get_the_date() ); ?></time>
            <?php if ( $category ) : ?>
                <span class="ml-dot">•</span>
                <a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>" style="color:inherit;text-decoration:none;"><?php echo esc_html( $category->name ); ?></a>
            <?php endif; ?>
        </div>

        <h2 class="blog-featured-title">
            <a href="<?php the_permalink(); ?>" style="color:inherit;text-decoration:none;"><?php the_title(); ?></a>
        </h2>

        <p class="blog-featured-excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 22, '...' ) ); ?></p>

        <div class="blog-author-row">
            <span><?php shopbar_icon( 'user', 14 ); ?> <?php esc_html_e( 'By', 'shopbar' ); ?> <?php the_author(); ?></span>
            <span><?php shopbar_icon( 'clock', 14 ); ?> <?php echo esc_html( $reading_time ); ?> <?php esc_html_e( 'min read', 'shopbar' ); ?></span>
        </div>

        <a href="<?php the_permalink(); ?>" class="blog-read-more">
            <?php esc_html_e( 'Read More', 'shopbar' ); ?> <?php shopbar_icon( 'arrow-right', 16 ); ?>
        </a>
    </div>
</article>
