<?php
/**
 * Featured Posts Template Part
 *
 * @package Peblog
 */

// Get featured posts based on customizer setting.
$featured_category = get_theme_mod( 'peblog_featured_category', '' );
$featured_args     = array( 'numberposts' => 3 );
if ( $featured_category ) {
	$featured_args['category_name'] = $featured_category;
}
$featured_posts = get_posts( $featured_args );
?>

<!-- related articles design start -->
<section id="related-articles" class="related-articles">
	<h2 class="heading"><?php echo esc_html( get_theme_mod( 'peblog_featured_title', __( 'Featured Posts', 'peblog' ) ) ); ?></h2>
	<div class="articles-container">
		<?php if ( empty( $featured_posts ) ) : ?>
			<div class="no-posts-message">
				<div class="no-posts-icon">
					<img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/icon/document.svg' ); ?>" alt="<?php esc_attr_e( 'No Posts', 'peblog' ); ?>" />
				</div>
				<h3><?php esc_html_e( 'No Featured Posts Found', 'peblog' ); ?></h3>
				<p><?php esc_html_e( 'There are no posts to display in the featured section. Please add some posts or check your category settings.', 'peblog' ); ?></p>
			</div>
		<?php else : ?>
			<?php
			foreach ( $featured_posts as $index => $featured_post ) :
				// Setup post data for template tags.
				setup_postdata( $featured_post );
				$author     = get_the_author_meta( 'display_name', $featured_post->post_author );
				$post_date  = get_the_date( 'd.m.Y', $featured_post->ID );
				$categories = get_the_category( $featured_post->ID );
				?>
			<article class="<?php echo 0 === $index ? 'left-article' : 'right-article'; ?>">
				<div class="related-post-card">
					<div class="card-image">
						<a href="<?php echo esc_url( get_permalink( $featured_post->ID ) ); ?>">
							<?php if ( has_post_thumbnail( $featured_post->ID ) ) : ?>
								<?php echo get_the_post_thumbnail( $featured_post->ID, 'large', array( 'class' => 'post-thumbnail' ) ); ?>
							<?php else : ?>
								<div class="post-placeholder">
									<div class="placeholder-icon">
										<img src="<?php echo esc_url( peblog_get_placeholder_icon( $featured_post->ID ) ); ?>" alt="<?php esc_attr_e( 'Post', 'peblog' ); ?>" />
									</div>
								</div>
							<?php endif; ?>
						</a>
					</div>
					<div class="card-content">
						<ul>
							<li class="post-author-image">
								<a href="<?php echo esc_url( get_author_posts_url( $featured_post->post_author ) ); ?>">
									<?php echo get_avatar( $featured_post->post_author, 32 ); ?>
								</a>
							</li>
							<li class="post-author"><a href="<?php echo esc_url( get_author_posts_url( $featured_post->post_author ) ); ?>"><?php echo esc_html( $author ); ?></a></li>
							<li class="post-date"><span><?php echo esc_html( $post_date ); ?></span></li>
						</ul>
						<h3 class="entry-title"><?php echo esc_html( $featured_post->post_title ); ?></h3>
						<?php if ( 0 === $index ) : // Only show excerpt for left-article. ?>
						<div class="entry-expert">
							<p><?php echo esc_html( wp_trim_words( $featured_post->post_content, 15, '...' ) ); ?></p>
						</div>
						<?php endif; ?>
						<div class="entry-meta">
							<div class="entry-tag">
								<?php if ( $categories ) : ?>
									<?php
									// Show only first category for right-article, all categories for left-article.
									$categories_to_show = ( 0 === $index ) ? $categories : array_slice( $categories, 0, 1 );

									// Dynamic color classes.
									$color_classes = array( 'tag-main', 'tag-warning', 'tag-success', 'tag-danger', 'tag-info' );
									?>
									<?php foreach ( $categories_to_show as $category ) : ?>
										<?php
										$color_index = $category->term_id % count( $color_classes );
										$color_class = $color_classes[ $color_index ];
										?>
										<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>" class="<?php echo esc_attr( $color_class ); ?>"><?php echo esc_html( $category->name ); ?></a>
									<?php endforeach; ?>
								<?php endif; ?>
							</div>
							<?php if ( function_exists( 'pvc_get_post_views' ) ) : ?>
								<div class="entry-rate">
									<a href="<?php echo esc_url( get_permalink( $featured_post->ID ) ); ?>">
										<img src="<?php echo esc_url( get_template_directory_uri() ); ?>/assets/images/icon/eye.svg" alt="<?php esc_attr_e( 'Views', 'peblog' ); ?>" />
										<?php echo esc_html( pvc_get_post_views( $featured_post->ID ) ); ?>
									</a>
								</div>
							<?php endif; ?>
						</div>
					</div>
				</div>
			</article>
			<?php endforeach; ?>
			<?php
			// Reset post data after the loop.
			wp_reset_postdata();
			?>
		<?php endif; ?>
	</div>
</section>
<!-- related articles design end -->
