<?php

/**
 * The template for displaying watchlist items in My Account
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/myaccount/content-watchlist.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 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_post_id = get_the_ID();
$movie_engine_poster = get_the_post_thumbnail_url($movie_engine_post_id, 'medium');
$movie_engine_title = get_the_title();
$movie_engine_post_type = get_post_type();
$movie_engine_type_label = ($movie_engine_post_type === 'movie_engine_series') ? esc_html__('TV Series', 'cine-movie-engine') : esc_html__('Movie', 'cine-movie-engine');
$movie_engine_years = get_the_terms($movie_engine_post_id, 'movie_engine_year');
$movie_engine_year = ($movie_engine_years && !is_wp_error($movie_engine_years)) ? $movie_engine_years[0]->name : '';
?>

<div class="movie-engine-movie-card me-watchlist-card" data-post-id="<?php echo esc_attr($movie_engine_post_id); ?>">
    <div class="movie-engine-card-poster">
        <a href="<?php the_permalink(); ?>" class="movie-engine-card-link">
            <?php if ($movie_engine_poster) : ?>
                <img src="<?php echo esc_url($movie_engine_poster); ?>" alt="<?php the_title_attribute(); ?>">
            <?php else : ?>
                <div class="movie-engine-poster-placeholder">
                    <span class="fa-solid fa-film"></span>
                </div>
            <?php endif; ?>
        </a>

        <button class="movie-engine-card-remove-btn me-remove-from-library" data-post-id="<?php echo esc_attr($movie_engine_post_id); ?>" title="<?php esc_html_e('Remove Watchlist', 'cine-movie-engine'); ?>">
            <span class="fa-solid fa-xmark"></span>
        </button>

        <?php
        if (is_user_logged_in()) {
            $movie_engine_user_id = get_current_user_id();
            $movie_engine_progress = \MovieEngine\Frontend\Users\WatchHistory::get_progress($movie_engine_user_id, $movie_engine_post_id);
            if ($movie_engine_progress > 0) {
                $movie_engine_duration = get_post_meta($movie_engine_post_id, '_movie_engine_duration_seconds', true);
                if (empty($movie_engine_duration)) {
                    $movie_engine_runtime = get_post_meta($movie_engine_post_id, '_movie_engine_runtime', true);
                    $movie_engine_duration = (!empty($movie_engine_runtime)) ? intval($movie_engine_runtime) * 60 : 7200;
                }
                if ($movie_engine_duration > 0) {
                    $movie_engine_percent = min(100, round(($movie_engine_progress / $movie_engine_duration) * 100));
                    if ($movie_engine_percent >= 1) {
                        echo '<div class="movie-engine-progress-bar"><div class="movie-engine-progress-fill" style="width: ' . esc_attr($movie_engine_percent) . '%;"></div></div>';
                    }
                }
            }
        }
        ?>
    </div>

    <div class="movie-engine-card-content">
        <h3 class="movie-engine-card-title">
            <a href="<?php the_permalink(); ?>"><?php echo esc_html($movie_engine_title); ?></a>
        </h3>
        <div class="movie-engine-card-meta">
            <?php if ($movie_engine_year) : ?>
                <span class="year"><?php echo esc_html($movie_engine_year); ?></span>
            <?php endif; ?>
            <span class="type"><?php echo esc_html($movie_engine_type_label); ?></span>
        </div>
    </div>
</div>