<?php

/**
 * The template for displaying content person
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/loop/content-person.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/loop
 * @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_ratio = $args['ratio'] ?? 'portrait';
$movie_engine_style = $args['style'] ?? 'classic';
$movie_engine_custom_ratio = $args['custom_ratio'] ?? '';

$movie_engine_poster = get_the_post_thumbnail_url($movie_engine_post_id, 'medium');
$movie_engine_known_for = get_post_meta($movie_engine_post_id, '_movie_engine_known_for', true);
$movie_engine_card_title = get_the_title();
$movie_engine_highlight_query = $args['highlight_query'] ?? '';

if (!empty($movie_engine_highlight_query)) {
    $movie_engine_card_title = preg_replace('/(' . preg_quote((string) $movie_engine_highlight_query, '/') . ')/i', '<mark>$1</mark>', (string) $movie_engine_card_title);
}
?>

<div class="movie-engine-movie-card movie-engine-person-card me-card-<?php echo esc_attr($movie_engine_ratio); ?> me-style-<?php echo esc_attr($movie_engine_style); ?>"
    <?php if ($movie_engine_ratio === 'custom' && !empty($movie_engine_custom_ratio)) {
        echo 'style="--me-custom-ratio: ' . esc_attr($movie_engine_custom_ratio) . ';"';
    } ?>>
    <a href="<?php the_permalink(); ?>" class="movie-engine-card-poster me-person-poster" aria-label="<?php the_title_attribute(); ?>">
        <?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 me-person-placeholder">
                <span class="fa-solid fa-user" aria-hidden="true"></span>
            </div>
        <?php endif; ?>
    </a>

    <div class="movie-engine-card-content">
        <h2 class="movie-engine-card-title">
            <a href="<?php the_permalink(); ?>"><?php echo \MovieEngine\Utils\Helpers::kses_post($movie_engine_card_title); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML ?></a>
        </h2>
        <div class="movie-engine-card-meta">
            <?php if ($movie_engine_known_for) : ?>
                <span class="me-person-dept"><?php echo esc_html($movie_engine_known_for); ?></span>
            <?php endif; ?>
        </div>
    </div>
</div>