<?php

/**
 * The template for displaying library item card
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/myaccount/content-library.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_post_id = get_the_ID();
$movie_engine_poster = get_the_post_thumbnail_url($movie_engine_post_id, 'medium');
$movie_engine_rating = get_post_meta($movie_engine_post_id, '_movie_engine_rating', true);
$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 : '';
$movie_engine_qualities = get_the_terms($movie_engine_post_id, 'movie_engine_quality');
$movie_engine_quality = ($movie_engine_qualities && !is_wp_error($movie_engine_qualities)) ? $movie_engine_qualities[0]->name : '';
$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');
?>

<div class="movie-engine-movie-card me-library-card" data-post-id="<?php echo esc_attr($movie_engine_post_id); ?>">
    <div class="movie-engine-card-poster-wrapper">
        <a href="<?php the_permalink(); ?>" class="movie-engine-card-poster">
            <?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; ?>

            <div class="movie-engine-card-overlay">
                <span class="fa-solid fa-play"></span>
            </div>

            <?php if ($movie_engine_quality) : ?>
                <span class="movie-engine-card-quality"><?php echo esc_html($movie_engine_quality); ?></span>
            <?php endif; ?>

            <?php if ($movie_engine_rating) : ?>
                <span class="movie-engine-card-rating">
                    <span class="fa-solid fa-star"></span> <?php echo esc_html($movie_engine_rating); ?>
                </span>
            <?php endif; ?>
        </a>
        <button class="movie-engine-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>
    </div>

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