<?php

/**
 * The template for displaying episode item in loops
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/loop/content-episode.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_ep_id = get_the_ID();
$movie_engine_ep_num = get_post_meta($movie_engine_ep_id, '_movie_engine_episode_number', true);
$movie_engine_ep_runtime = get_post_meta($movie_engine_ep_id, '_movie_engine_runtime', true);
$movie_engine_ep_air_date = get_post_meta($movie_engine_ep_id, '_movie_engine_air_date', true);
$movie_engine_ep_thumb = get_the_post_thumbnail_url($movie_engine_ep_id, 'medium');
if (!$movie_engine_ep_thumb) {
    $movie_engine_series_id = get_post_meta($movie_engine_ep_id, '_movie_engine_series_id', true);
    if ($movie_engine_series_id) {
        $movie_engine_ep_thumb = get_post_meta($movie_engine_series_id, '_movie_engine_backdrop_url', true);
        if (!$movie_engine_ep_thumb) {
            $movie_engine_ep_thumb = get_the_post_thumbnail_url($movie_engine_series_id, 'medium');
        }
    }
}
$movie_engine_ep_placeholder = MOVIE_ENGINE_URL . 'assets/images/episode-placeholder.svg';
$movie_engine_display_thumb = $movie_engine_ep_thumb ?: $movie_engine_ep_placeholder;

$movie_engine_is_watched = false;
if (is_user_logged_in()) {
    $movie_engine_user_id = get_current_user_id();
    $movie_engine_watched_list = get_user_meta($movie_engine_user_id, '_movie_engine_watched', true);
    if (is_array($movie_engine_watched_list) && in_array($movie_engine_ep_id, $movie_engine_watched_list)) {
        $movie_engine_is_watched = true;
    }
}
?>

<div class="movie-engine-episode-item-premium <?php echo esc_attr($movie_engine_is_watched ? 'is-watched' : ''); ?>" data-id="<?php echo esc_attr($movie_engine_ep_id); ?>">
    <div class="movie-engine-ep-left">
        <div class="movie-engine-ep-thumbnail">
            <img src="<?php echo esc_url($movie_engine_display_thumb); ?>" alt="<?php the_title_attribute(); ?>">
            <span class="movie-engine-ep-index"><?php echo esc_html($movie_engine_ep_num); ?></span>
            <div class="movie-engine-ep-thumbnail-overlay">
                <span class="fa-solid fa-play"></span>
            </div>
        </div>

        <div class="movie-engine-ep-details">
            <div class="movie-engine-ep-title-row">
                <span class="movie-engine-ep-index-mobile"><?php echo esc_html($movie_engine_ep_num); ?>.</span>
                <h3 class="movie-engine-ep-title"><?php echo esc_html(get_the_title()); ?></h3>
            </div>
            <div class="movie-engine-ep-meta">
                <?php if ($movie_engine_ep_runtime): ?>
                    <span class="movie-engine-ep-runtime"><?php echo esc_html($movie_engine_ep_runtime); ?>m</span>
                <?php endif; ?>

                <?php if ($movie_engine_ep_air_date): ?>
                    <span class="movie-engine-ep-meta-sep"></span>
                    <span class="movie-engine-ep-air-date"><?php echo esc_html(date_i18n(get_option('date_format'), strtotime($movie_engine_ep_air_date))); ?></span>
                <?php endif; ?>

                <?php if ($movie_engine_is_watched): ?>
                    <span class="movie-engine-ep-watched-badge">
                        <span class="fa-solid fa-check"></span>
                        <?php esc_html_e('Watched', 'cine-movie-engine'); ?>
                    </span>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div class="movie-engine-ep-right">
        <?php if ($movie_engine_ep_runtime): ?>
            <span class="movie-engine-ep-runtime-desktop"><?php echo esc_html($movie_engine_ep_runtime); ?>m</span>
        <?php endif; ?>

        <?php if ($movie_engine_ep_air_date): ?>
            <span class="movie-engine-ep-date-desktop"><?php echo esc_html(date_i18n(get_option('date_format'), strtotime($movie_engine_ep_air_date))); ?></span>
        <?php endif; ?>

        <a href="<?php the_permalink(); ?>" class="movie-engine-btn me-btn-play-ep">
            <span class="fa-solid fa-play"></span>
            <?php esc_html_e('Play', 'cine-movie-engine'); ?>
        </a>
    </div>
</div>