<?php

/**
 * The template for displaying episode sidebar trending section
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/single-episode/sidebar-trending.php.
 *
 * HOWEVER, on occasion MovieEngine will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @note Please do not delete our CSS classes when you customize, as it may break functionality.
 * @package Movie_Engine
 * @subpackage Movie_Engine/templates
 * @version 1.0.0
 * @since 1.0.0
 */
if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

$movie_engine_sidebar_trending_options = get_option('movie_engine_design_settings', []);
$movie_engine_show_trending = isset($movie_engine_sidebar_trending_options['episode_show_trending']) ? (bool)$movie_engine_sidebar_trending_options['episode_show_trending'] : true;

if (!$movie_engine_show_trending) {
    return;
}

$movie_engine_trending_title = $movie_engine_sidebar_trending_options['episode_trending_title'] ?? esc_html__('Popular TV Shows', 'cine-movie-engine');
$movie_engine_trending_count = isset($movie_engine_sidebar_trending_options['episode_trending_count']) ? intval($movie_engine_sidebar_trending_options['episode_trending_count']) : 5;

$movie_engine_trending_query = \MovieEngine\Frontend\Query::get_posts_by_type('trending', $movie_engine_trending_count, 0, 'movie_engine_series');

if ($movie_engine_trending_query && $movie_engine_trending_query->have_posts()) :
?>
    <div class="movie-engine-sidebar-trending">
        <div class="me-sidebar-section-title">
            <h3><?php echo esc_html($movie_engine_trending_title); ?></h3>
        </div>
        <div class="me-sidebar-trending-list">
            <?php
            $movie_engine_rank = 1;
            while ($movie_engine_trending_query->have_posts()) : $movie_engine_trending_query->the_post();
                $movie_engine_item_post_id = get_the_ID();
                $movie_engine_thumb = get_the_post_thumbnail_url($movie_engine_item_post_id, 'medium');
                $movie_engine_year = get_post_meta($movie_engine_item_post_id, '_movie_engine_year', true);
                $movie_engine_genres = get_the_term_list($movie_engine_item_post_id, 'movie_engine_genre', '', '  •  ', '');
            ?>
                <a href="<?php the_permalink(); ?>" class="me-sidebar-trending-item">
                    <div class="me-item-rank"><?php echo esc_html($movie_engine_rank++); ?></div>
                    <?php if ($movie_engine_thumb) : ?>
                        <div class="me-item-thumb">
                            <img src="<?php echo esc_url($movie_engine_thumb); ?>" alt="<?php the_title_attribute(); ?>">
                        </div>
                    <?php endif; ?>
                    <div class="me-item-info">
                        <div class="me-meta-top">
                            <?php
                            $movie_engine_rating_val = get_post_meta($movie_engine_item_post_id, '_movie_engine_imdb_rating', true);
                            if (!$movie_engine_rating_val || $movie_engine_rating_val === 'N/A') {
                                $movie_engine_rating_val = get_post_meta($movie_engine_item_post_id, '_movie_engine_rating', true);
                            }
                            if ($movie_engine_year) : ?>
                                <span class="me-year"><?php echo esc_html($movie_engine_year); ?></span>
                            <?php endif; ?>
                            <?php if ($movie_engine_rating_val && $movie_engine_rating_val > 0) : ?>
                                <?php if ($movie_engine_year) echo '<span class="me-sep-dot"></span>'; ?>
                                <span class="me-rating">★ <?php echo esc_html(is_numeric($movie_engine_rating_val) ? number_format((float)$movie_engine_rating_val, 1) : $movie_engine_rating_val); ?></span>
                            <?php endif; ?>
                        </div>
                        <h5 class="me-item-title"><?php the_title(); ?></h5>
                        <div class="me-meta-bottom">
                            <?php
                            $movie_engine_status_val = get_post_meta($movie_engine_item_post_id, '_movie_engine_status', true);
                            if ($movie_engine_status_val) : ?>
                                <span class="me-status"><?php echo esc_html(strtoupper($movie_engine_status_val)); ?></span>
                            <?php endif; ?>
                            <?php if ($movie_engine_genres) : ?>
                                <?php if ($movie_engine_status_val) echo '<span class="me-sep-dot"></span>'; ?>
                                <span class="me-genres"><?php echo esc_html(wp_strip_all_tags($movie_engine_genres)); ?></span>
                            <?php endif; ?>
                        </div>
                    </div>
                </a>
            <?php endwhile;
            wp_reset_postdata(); ?>
        </div>
    </div>
<?php endif; ?>