<?php

/**
 * The template for displaying social action buttons
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/global/actions-social.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 = isset($args['post_id']) ? $args['post_id'] : get_the_ID();
$movie_engine_views = (int) get_post_meta($movie_engine_post_id, '_movie_engine_views', true);
$movie_engine_likes = (int) get_post_meta($movie_engine_post_id, '_movie_engine_likes_count', true);

$movie_engine_liked = false;
if (is_user_logged_in()) {
    $movie_engine_user_likes = get_user_meta(get_current_user_id(), '_movie_engine_liked_posts', true);
    if (is_array($movie_engine_user_likes) && in_array($movie_engine_post_id, $movie_engine_user_likes)) {
        $movie_engine_liked = true;
    }
}

$movie_engine_likes_label = ($movie_engine_likes > 0) ? sprintf(
    /* translators: %s: number of likes formatted */
    _n('%s Like', '%s Likes', $movie_engine_likes, 'cine-movie-engine'),
    \MovieEngine\Utils\Helpers::format_number($movie_engine_likes)
) : esc_html__('Like', 'cine-movie-engine');
?>

<div class="movie-engine-social-actions">
    <button class="movie-engine-action-btn me-like-btn <?php echo esc_attr($movie_engine_liked ? 'active' : ''); ?>"
        data-post-id="<?php echo esc_attr($movie_engine_post_id); ?>"
        data-count="<?php echo (int) $movie_engine_likes; ?>"
        data-tooltip="<?php echo esc_attr($movie_engine_likes_label); ?>"
        aria-label="<?php echo esc_attr($movie_engine_likes_label); ?>">
        <span class="fa-solid fa-heart" aria-hidden="true"></span>
        <span class="movie-engine-count"><?php echo esc_html(\MovieEngine\Utils\Helpers::format_number($movie_engine_likes)); ?></span>
    </button>
    <button class="movie-engine-action-btn me-share-btn"
        data-url="<?php echo esc_url(get_permalink($movie_engine_post_id)); ?>"
        data-title="<?php echo esc_attr(get_the_title($movie_engine_post_id)); ?>"
        data-tooltip="<?php esc_html_e('Share', 'cine-movie-engine'); ?>"
        aria-label="<?php esc_attr_e('Share', 'cine-movie-engine'); ?>">
        <span class="fa-solid fa-share-nodes" aria-hidden="true"></span>
    </button>
</div>