<?php

/**
 * The template for displaying ratings in single movie
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/single-movie/rating.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_imdb_rating = get_post_meta($movie_engine_post_id, '_movie_engine_imdb_rating', true);
$movie_engine_tmdb_rating = get_post_meta($movie_engine_post_id, '_movie_engine_rating', true);
$movie_engine_tomatoes = get_post_meta($movie_engine_post_id, '_movie_engine_tomatoes_rating', true);
$movie_engine_metacritic = get_post_meta($movie_engine_post_id, '_movie_engine_metacritic_rating', true);
$movie_engine_user_rating = get_post_meta($movie_engine_post_id, '_movie_engine_user_rating', true);

if (!$movie_engine_imdb_rating && !$movie_engine_tmdb_rating && !$movie_engine_tomatoes && !$movie_engine_metacritic && !$movie_engine_user_rating) return;

if ($movie_engine_imdb_rating == $movie_engine_tmdb_rating) {
    $movie_engine_imdb_rating = null;
}
?>

<div class="movie-engine-ratings-container">
    <?php if ($movie_engine_user_rating && $movie_engine_user_rating > 0) : ?>
        <div class="movie-engine-rating-item users" title="<?php esc_html_e('User Rating', 'cine-movie-engine'); ?>">
            <span class="movie-engine-rating-logo"><span class="fa-solid fa-star"></span></span>
            <span class="movie-engine-rating-value"><?php echo esc_html(number_format((float)$movie_engine_user_rating, 1)); ?></span>
        </div>
    <?php endif; ?>

    <?php if ($movie_engine_imdb_rating && $movie_engine_imdb_rating !== 'N/A') : ?>
        <div class="movie-engine-rating-item imdb" title="<?php esc_html_e('IMDb Rating', 'cine-movie-engine'); ?>">
            <span class="movie-engine-rating-logo"><?php esc_html_e('IMDb', 'cine-movie-engine'); ?></span>
            <span class="movie-engine-rating-value"><?php echo esc_html($movie_engine_imdb_rating); ?></span>
        </div>
    <?php endif; ?>

    <?php if ($movie_engine_tmdb_rating && $movie_engine_tmdb_rating > 0) : ?>
        <div class="movie-engine-rating-item tmdb" title="<?php esc_html_e('TMDb Rating', 'cine-movie-engine'); ?>">
            <span class="movie-engine-rating-logo"><?php esc_html_e('TMDb', 'cine-movie-engine'); ?></span>
            <span class="movie-engine-rating-value"><?php echo esc_html(number_format((float)$movie_engine_tmdb_rating, 1)); ?></span>
        </div>
    <?php endif; ?>


    <?php if ($movie_engine_tomatoes) : ?>
        <div class="movie-engine-rating-item tomatoes" title="<?php esc_html_e('Rotten Tomatoes', 'cine-movie-engine'); ?>">
            <span class="movie-engine-rating-logo">
                <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M8 14.5C11.5899 14.5 14.5 11.5899 14.5 8C14.5 4.41015 11.5899 1.5 8 1.5C4.41015 1.5 1.5 4.41015 1.5 8C1.5 11.5899 4.41015 14.5 8 14.5Z" fill="#F93309" />
                    <path d="M8.5 2.5C8.5 2.22386 8.27614 2 8 2C7.72386 2 7.5 2.22386 7.5 2.5V3.5C7.5 3.77614 7.72386 4 8 4C8.27614 4 8.5 3.77614 8.5 3.5V2.5Z" fill="#15803D" />
                </svg>
            </span>
            <span class="movie-engine-rating-value"><?php echo esc_html($movie_engine_tomatoes); ?></span>
        </div>
    <?php endif; ?>

    <?php if ($movie_engine_metacritic) : ?>
        <div class="movie-engine-rating-item metacritic" title="<?php esc_html_e('Metacritic', 'cine-movie-engine'); ?>">
            <span class="movie-engine-rating-logo"><?php esc_html_e('MC', 'cine-movie-engine'); ?></span>
            <span class="movie-engine-rating-value"><?php echo esc_html($movie_engine_metacritic); ?></span>
        </div>
    <?php endif; ?>
</div>