<?php

namespace MovieEngine\Frontend;

if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

class Design {

    public function __construct() {
        add_action('wp_footer', [$this, 'render_trailer_modal'], 5);
        add_action('wp_footer', [$this, 'render_details_modal']);
        add_action('wp_footer', [$this, 'render_interaction_modals'], 20);
        add_filter('body_class', [$this, 'add_dynamic_body_classes']);
        $this->add_content_filters();
    }

    /**
     * Renders the trailer popup modal in footer (same pattern as other modals).
     * Output on all frontend pages so hero shortcode and any .me-open-trailer trigger can open it.
     */
    public function render_trailer_modal() {
        if (is_admin()) {
            return;
        }

        \MovieEngine\Utils\TemplateLoader::get_template_part('global/modal-trailer');
    }

    private function add_content_filters() {
        $filters = [
            'wptexturize',
            'convert_smilies',
            'convert_chars',
            'wpautop',
            'shortcode_unautop',
            'do_shortcode',
            'wp_filter_content_tags',
        ];
        foreach ($filters as $filter) {
            add_filter('movie_engine_the_content', $filter);
        }
    }

    public function render_details_modal() {
        $is_playlist_page = get_query_var('playlist');
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $is_dashboard_view = isset($_GET['movie_engine_view']);
        $post = get_post();
        $has_dashboard = $post && has_shortcode($post->post_content, 'movie_engine_dashboard');

        if (!is_singular(['movie_engine_movie', 'movie_engine_series', 'movie_engine_episode']) && !$is_playlist_page && !$has_dashboard && !$is_dashboard_view) {
            return;
        }

        $post_id = get_the_ID();

        if ($is_playlist_page) {
            $user_id = get_current_user_id();
            $playlists = \MovieEngine\Frontend\Users\UserLibrary::get_playlists($user_id);
            if (isset($playlists[$is_playlist_page])) {
                $items = isset($playlists[$is_playlist_page]['items']) ? (array) $playlists[$is_playlist_page]['items'] : [];
                $v_index = (int) get_query_var('v', 0);
                if (isset($items[$v_index])) {
                    $post_id = $items[$v_index];
                }
            }
        }

        $post = get_post($post_id);
        $poster = get_the_post_thumbnail_url($post_id, 'large');
        $title = get_the_title($post_id);
        $content = apply_filters('movie_engine_the_content', $post->post_content);

        $year = get_post_meta($post_id, '_movie_engine_year', true);
        $runtime = get_post_meta($post_id, '_movie_engine_runtime', true);
        $status = get_post_meta($post_id, '_movie_engine_status', true);
        $cast = get_post_meta($post_id, '_movie_engine_cast', true);

        $imdb_rating = get_post_meta($post_id, '_movie_engine_rating', true);
        $user_rating = get_post_meta($post_id, '_movie_engine_user_rating', true);
        $imdb_id = get_post_meta($post_id, '_movie_engine_imdb_id', true);

        $release_date = get_post_meta($post_id, '_movie_engine_release_date', true);
        $mpaa = get_post_meta($post_id, '_movie_engine_mpaa', true);
        $tagline = get_post_meta($post_id, '_movie_engine_tagline', true);
        $budget = get_post_meta($post_id, '_movie_engine_budget', true);
        $revenue = get_post_meta($post_id, '_movie_engine_revenue', true);
        $production = get_post_meta($post_id, '_movie_engine_production', true);

        $directors_data = get_post_meta($post_id, '_movie_engine_directors_data', true);
        $creators_data = get_post_meta($post_id, '_movie_engine_creators_data', true);

        $views = get_post_meta($post_id, '_movie_engine_views', true);
        $likes = get_post_meta($post_id, '_movie_engine_likes_count', true);

        $is_episode = get_post_type($post_id) === 'movie_engine_episode';
        $series_id = $is_episode ? get_post_meta($post_id, '_movie_engine_series_id', true) : 0;
        $season_num = $is_episode ? get_post_meta($post_id, '_movie_engine_season_number', true) : '';
        $ep_num = $is_episode ? get_post_meta($post_id, '_movie_engine_episode_number', true) : '';

        if ($is_episode) {
            if (empty($runtime)) {
                $runtime = get_post_meta($post_id, '_movie_engine_duration', true);
            }
            if (empty($release_date)) {
                $release_date = get_post_meta($post_id, '_movie_engine_air_date', true);
            }
            if (empty($year) && $release_date) {
                $year = gmdate('Y', strtotime($release_date));
            }
            if (empty($cast) && $series_id) {
                $cast = get_post_meta($series_id, '_movie_engine_cast', true);
            }
            if (empty($mpaa) && $series_id) {
                $mpaa = get_post_meta($series_id, '_movie_engine_mpaa', true);
            }
        }

        $director = get_the_term_list($post_id, 'movie_engine_director', '', ', ');
        $director = is_wp_error($director) ? '' : $director;

        $genres = get_the_term_list($post_id, 'movie_engine_genre', '', '', '');
        $genres = is_wp_error($genres) ? '' : $genres;

        $country = get_the_term_list($post_id, 'movie_engine_country', '', ', ');
        $country = is_wp_error($country) ? '' : $country;

        $language = get_the_term_list($post_id, 'movie_engine_language', '', ', ');
        $language = is_wp_error($language) ? '' : $language;

        $writer = get_the_term_list($post_id, 'movie_engine_writer', '', ', ');
        $writer = is_wp_error($writer) ? '' : $writer;

        $network = get_the_term_list($post_id, 'movie_engine_network', '', ', ');
        $network = is_wp_error($network) ? '' : $network;

        $actors = get_the_term_list($post_id, 'movie_engine_actor', '', ', ');
        $actors = is_wp_error($actors) ? '' : $actors;

        $is_tv = is_singular('movie_engine_series') || is_singular('movie_engine_episode');
        $quality_tax = $is_tv ? 'movie_engine_tv_quality' : 'movie_engine_quality';
        $quality_terms = get_the_term_list($post_id, $quality_tax, '', ', ');
        $quality = is_wp_error($quality_terms) ? '' : wp_strip_all_tags($quality_terms);
?>
        <div id="movie-engine-details-modal" class="movie-engine-modal me-hidden me-movie-details-modal">
            <div class="movie-engine-modal-content me-details-modal-content me-p-4">
                <span class="movie-engine-close-modal">&times;</span>
                <div class="me-modal-body">
                    <div class="me-modal-poster">
                        <img src="<?php echo esc_url($poster); ?>" alt="<?php echo esc_attr($title); ?>">
                    </div>
                    <div class="me-modal-info">
                        <?php if ($is_episode && $series_id) : ?>
                            <div class="me-modal-breadcrumb">
                                <span class="me-series-title"><?php echo esc_html(get_the_title($series_id)); ?></span>
                                <?php if ($season_num || $ep_num) : ?>
                                    <span class="me-sep">/</span>
                                    <span class="me-ep-count">
                                        <?php
                                        if ($season_num) {
                                            printf(
                                                /* translators: %d: season number */
                                                esc_html__('Season %d', 'cine-movie-engine'),
                                                (int) $season_num
                                            );
                                        }

                                        if ($season_num && $ep_num) {
                                            echo ' - ';
                                        }

                                        if ($ep_num) {
                                            printf(
                                                /* translators: %d: episode number */
                                                esc_html__('Episode %d', 'cine-movie-engine'),
                                                (int) $ep_num
                                            );
                                        }

                                        ?>
                                    </span>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>

                        <?php if ($tagline) : ?>
                            <p class="me-modal-tagline"><?php echo esc_html($tagline); ?></p>
                        <?php endif; ?>

                        <h2><?php echo esc_html($title); ?></h2>

                        <div class="me-modal-ratings-row">
                            <?php if ($imdb_rating && $imdb_rating > 0) : ?>
                                <div class="me-rating-item me-imdb-rating">
                                    <svg width="40" height="20" viewBox="0 0 64 32" fill="none" xmlns="http://www.w3.org/2000/svg">
                                        <rect width="64" height="32" rx="4" fill="var(--movie-engine-yellow-color, #F5C518)" />
                                        <path d="M8 8h3v16H8V8zm6 0h4.5l2 10.5L22.5 8H27v16h-3V12.5L21.5 24h-2L17 12.5V24h-3V8zm14 0h6c2.5 0 4 1.5 4 4v8c0 2.5-1.5 4-4 4h-6V8zm3 3v10h2.5c1 0 1.5-.5 1.5-1.5v-7c0-1-.5-1.5-1.5-1.5H31zm11-3h6c2 0 3 1 3 3v2c0 1.5-.5 2.5-1.5 3 1 .5 1.5 1.5 1.5 3v2c0 2-1 3-3 3h-6V8zm3 3v4h2c.5 0 1-.5 1-1v-2c0-.5-.5-1-1-1h-2zm0 7v4h2c.5 0 1-.5 1-1v-2c0-.5-.5-1-1-1h-2z" fill="#000" />
                                    </svg>
                                    <span class="me-rating-value"><?php echo esc_html(number_format((float)$imdb_rating, 1)); ?></span>
                                    <span class="me-rating-max">/10</span>
                                </div>
                            <?php endif; ?>

                            <?php if ($user_rating && $user_rating > 0) : ?>
                                <div class="me-rating-item me-user-rating">
                                    <span class="me-rating-icon" style="color: var(--movie-engine-primary-color);">★</span>
                                    <span class="me-rating-value"><?php echo esc_html(number_format((float)$user_rating, 1)); ?></span>
                                    <span class="me-rating-label"><?php esc_html_e('User', 'cine-movie-engine'); ?></span>
                                </div>
                            <?php endif; ?>

                            <?php if ($views && $views > 0) : ?>
                                <div class="me-rating-item me-views-stat">
                                    <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor">
                                        <path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z" />
                                    </svg>
                                    <span class="me-rating-value"><?php echo esc_html(number_format($views)); ?></span>
                                    <span class="me-rating-label"><?php esc_html_e('Views', 'cine-movie-engine'); ?></span>
                                </div>
                            <?php endif; ?>

                            <?php if ($likes && $likes > 0) : ?>
                                <div class="me-rating-item me-likes-stat">
                                    <svg width="18" height="18" viewBox="0 0 24 24" fill="var(--movie-engine-primary-color)">
                                        <path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z" />
                                    </svg>
                                    <span class="me-rating-value"><?php echo esc_html(number_format($likes)); ?></span>
                                    <span class="me-rating-label"><?php esc_html_e('Likes', 'cine-movie-engine'); ?></span>
                                </div>
                            <?php endif; ?>
                        </div>

                        <div class="me-modal-meta-row">
                            <?php if ($year) : ?><span><?php echo esc_html($year); ?></span><?php endif; ?>
                            <?php if ($runtime) : ?>
                                <span class="me-dot">•</span>
                                <span><?php echo esc_html($runtime); ?> <?php esc_html_e('Min', 'cine-movie-engine'); ?></span>
                            <?php endif; ?>
                            <?php if ($quality) : ?>
                                <span class="me-dot">•</span>
                                <span class="me-quality-badge"><?php echo esc_html($quality); ?></span>
                            <?php endif; ?>
                            <?php if ($mpaa) : ?>
                                <span class="me-dot">•</span>
                                <span class="me-mpaa-badge"><?php echo esc_html($mpaa); ?></span>
                            <?php endif; ?>
                            <?php if ($status) : ?>
                                <span class="me-dot">•</span>
                                <span class="me-status-badge"><?php echo esc_html($status); ?></span>
                            <?php endif; ?>
                        </div>

                        <div class="me-modal-tabs">
                            <span class="active" data-tab="overview"><?php esc_html_e('Overview', 'cine-movie-engine'); ?></span>
                            <span data-tab="details"><?php esc_html_e('Details', 'cine-movie-engine'); ?></span>
                            <?php if (!empty($cast) && is_array($cast)) : ?>
                                <span data-tab="cast"><?php esc_html_e('Cast', 'cine-movie-engine'); ?></span>
                            <?php endif; ?>
                        </div>

                        <div class="me-modal-scroll-area">
                            <div class="me-modal-tab-content active" data-tab-content="overview">
                                <?php if ($genres) : ?>
                                    <div class="me-modal-genres"><?php echo \MovieEngine\Utils\Helpers::kses_post($genres); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML ?></div>
                                <?php endif; ?>

                                <div class="me-modal-synopsis">
                                    <?php echo \MovieEngine\Utils\Helpers::kses_post($content); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML ?>
                                </div>
                            </div>

                            <div class="me-modal-tab-content" data-tab-content="details">
                                <div class="me-modal-details-list">
                                    <?php if ($is_episode && $series_id) : ?>
                                        <p><strong><?php esc_html_e('Series:', 'cine-movie-engine'); ?></strong> <a href="<?php echo esc_url(get_permalink($series_id)); ?>"><?php echo esc_html(get_the_title($series_id)); ?></a></p>
                                    <?php endif; ?>
                                    <?php if ($is_episode && $season_num) : ?>
                                        <p><strong><?php esc_html_e('Season:', 'cine-movie-engine'); ?></strong> <?php echo esc_html($season_num); ?></p>
                                    <?php endif; ?>
                                    <?php if ($is_episode && $ep_num) : ?>
                                        <p><strong><?php esc_html_e('Episode:', 'cine-movie-engine'); ?></strong> <?php echo esc_html($ep_num); ?></p>
                                    <?php endif; ?>
                                    <?php if ($release_date) : ?>
                                        <p><strong><?php echo $is_episode ? esc_html__('Air Date:', 'cine-movie-engine') : esc_html__('Release Date:', 'cine-movie-engine'); ?></strong> <?php echo esc_html(date_i18n(get_option('date_format'), strtotime($release_date))); ?></p>
                                    <?php endif; ?>
                                    <?php if ($genres) : ?>
                                        <p><strong><?php esc_html_e('Genres:', 'cine-movie-engine'); ?></strong> <?php echo esc_html(wp_strip_all_tags($genres)); ?></p>
                                    <?php endif; ?>
                                    <?php if ($director || !empty($directors_data)) : ?>
                                        <p><strong><?php esc_html_e('Director:', 'cine-movie-engine'); ?></strong>
                                            <?php
                                            if (!empty($directors_data)) {
                                                $d_links = [];
                                                foreach ($directors_data as $d) {
                                                    $d_url = isset($d['id']) && $d['id'] ? get_permalink($d['id']) : '';
                                                    $d_links[] = $d_url ? '<a href="' . esc_url($d_url) . '">' . esc_html($d['name']) . '</a>' : esc_html($d['name']);
                                                }
                                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML
                                                echo \MovieEngine\Utils\Helpers::kses_post(implode(', ', $d_links));
                                            } else {
                                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML
                                                echo \MovieEngine\Utils\Helpers::kses_post($director);
                                            }
                                            ?>
                                        </p>
                                    <?php endif; ?>

                                    <?php if ($writer) : ?>
                                        <p><strong><?php esc_html_e('Writer:', 'cine-movie-engine'); ?></strong> <?php echo \MovieEngine\Utils\Helpers::kses_post($writer); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML ?></p>
                                    <?php endif; ?>

                                    <?php if ($actors || !empty($creators_data)) : ?>
                                        <p><strong><?php esc_html_e('Stars:', 'cine-movie-engine'); ?></strong>
                                            <?php
                                            if (!empty($creators_data)) {
                                                $c_links = [];
                                                foreach ($creators_data as $c) {
                                                    $c_url = isset($c['id']) && $c['id'] ? get_permalink($c['id']) : '';
                                                    $c_links[] = $c_url ? '<a href="' . esc_url($c_url) . '">' . esc_html($c['name']) . '</a>' : esc_html($c['name']);
                                                }
                                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML
                                                echo \MovieEngine\Utils\Helpers::kses_post(implode(', ', $c_links));
                                            } else {
                                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML
                                                echo \MovieEngine\Utils\Helpers::kses_post($actors);
                                            }
                                            ?>
                                        </p>
                                    <?php endif; ?>
                                    <?php if ($language) : ?>
                                        <p><strong><?php esc_html_e('Language:', 'cine-movie-engine'); ?></strong> <?php echo esc_html(wp_strip_all_tags($language)); ?></p>
                                    <?php endif; ?>
                                    <?php if ($country) : ?>
                                        <p><strong><?php esc_html_e('Country:', 'cine-movie-engine'); ?></strong> <?php echo esc_html(wp_strip_all_tags($country)); ?></p>
                                    <?php endif; ?>
                                    <?php if ($network) : ?>
                                        <p><strong><?php esc_html_e('Network:', 'cine-movie-engine'); ?></strong> <?php echo esc_html(wp_strip_all_tags($network)); ?></p>
                                    <?php endif; ?>
                                    <?php if ($production) : ?>
                                        <p><strong><?php esc_html_e('Production:', 'cine-movie-engine'); ?></strong> <?php echo esc_html($production); ?></p>
                                    <?php endif; ?>
                                    <?php if ($budget) : ?>
                                        <p><strong><?php esc_html_e('Budget:', 'cine-movie-engine'); ?></strong> $<?php echo esc_html(number_format($budget)); ?></p>
                                    <?php endif; ?>
                                    <?php if ($revenue) : ?>
                                        <p><strong><?php esc_html_e('Revenue:', 'cine-movie-engine'); ?></strong> $<?php echo esc_html(number_format($revenue)); ?></p>
                                    <?php endif; ?>
                                    <?php if ($imdb_id) : ?>
                                        <p><strong><?php esc_html_e('IMDb:', 'cine-movie-engine'); ?></strong> <a href="https://www.imdb.com/title/<?php echo esc_attr($imdb_id); ?>" target="_blank" rel="noopener"><?php echo esc_html($imdb_id); ?></a></p>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <?php if (!empty($cast) && is_array($cast)) : ?>
                                <div class="me-modal-tab-content" data-tab-content="cast">
                                    <div class="me-modal-cast-section">
                                        <div class="me-cast-grid">
                                            <?php
                                            $count = 0;
                                            foreach ($cast as $actor) {
                                                if ($count++ >= 20) break;

                                                // Priority: Linked Person > Manual Data
                                                $person_id = isset($actor['id']) ? absint($actor['id']) : 0;
                                                $name = isset($actor['name']) ? $actor['name'] : '';
                                                $character = isset($actor['character']) ? $actor['character'] : '';
                                                $profile = isset($actor['photo']) && !empty($actor['photo']) ? $actor['photo'] : '';
                                                $person_url = '';

                                                if ($person_id && get_post_status($person_id)) {
                                                    $name = get_the_title($person_id);
                                                    $person_url = get_permalink($person_id);
                                                    if (has_post_thumbnail($person_id)) {
                                                        $profile = get_the_post_thumbnail_url($person_id, 'medium');
                                                    }
                                                }

                                                if (empty($name)) continue;
                                            ?>
                                                <div class="me-cast-item">
                                                    <?php if ($person_url) : ?><a href="<?php echo esc_url($person_url); ?>" class="me-cast-link"><?php endif; ?>
                                                        <?php if ($profile) : ?>
                                                            <img src="<?php echo esc_url($profile); ?>" alt="<?php echo esc_attr($name); ?>" class="me-cast-photo">
                                                        <?php else : ?>
                                                            <div class="me-cast-photo me-no-photo">
                                                                <svg viewBox="0 0 24 24" fill="currentColor">
                                                                    <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z" />
                                                                </svg>
                                                            </div>
                                                        <?php endif; ?>
                                                        <?php if ($person_url) : ?>
                                                        </a><?php endif; ?>

                                                    <div class="me-cast-info">
                                                        <span class="me-cast-name">
                                                            <?php if ($person_url) : ?><a href="<?php echo esc_url($person_url); ?>"><?php endif; ?>
                                                                <?php echo esc_html($name); ?>
                                                                <?php if ($person_url) : ?></a><?php endif; ?>
                                                        </span>
                                                        <?php if ($character) : ?>
                                                            <span class="me-cast-character"><?php echo esc_html($character); ?></span>
                                                        <?php endif; ?>
                                                    </div>
                                                </div>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div id="movie-engine-share-modal" class="movie-engine-modal me-hidden me-movie-share-modal">
            <div class="movie-engine-modal-content me-share-modal-modern">
                <span class="movie-engine-close-modal">&times;</span>

                <div class="me-share-header">
                    <h3><?php
                        if (apply_filters('movie_engine_is_pro', false)) {
                            esc_html_e('Share Your Link & Earn', 'cine-movie-engine');
                        } else {
                            esc_html_e('Share Current Item', 'cine-movie-engine');
                        }
                        ?></h3>
                    <p><?php
                        if (apply_filters('movie_engine_is_pro', false)) {
                            esc_html_e('Copy your unique referral link and share it with your friends.', 'cine-movie-engine');
                        } else {
                            esc_html_e('Copy link and share it with your friends.', 'cine-movie-engine');
                        }
                        ?></p>
                </div>

                <div class="me-share-url-container">
                    <div class="me-share-input-wrapper">
                        <?php
                        $share_link = apply_filters('movie_engine_share_link', get_permalink($post_id), $post_id);
                        ?>
                        <input type="text" readonly value="<?php echo esc_attr($share_link); ?>" id="movie-engine-share-url-input">
                        <button id="movie-engine-copy-link-btn"><?php esc_html_e('Copy', 'cine-movie-engine'); ?></button>
                    </div>
                </div>

                <div class="me-social-share-grid">
                    <?php
                    $share_url = urlencode(get_permalink($post_id));
                    $share_title = urlencode(get_the_title($post_id));
                    ?>
                    <a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo esc_attr($share_url); ?>" target="_blank" class="me-social-icon facebook" title="Facebook">
                        <svg viewBox="0 0 24 24">
                            <path d="M18 2h-3a5 5 0 00-5 5v3H7v4h3v8h4v-8h3l1-4h-4V7a1 1 0 011-1h3z"></path>
                        </svg>
                    </a>
                    <a href="https://twitter.com/intent/tweet?url=<?php echo esc_attr($share_url); ?>&text=<?php echo esc_attr($share_title); ?>" target="_blank" class="me-social-icon twitter" title="Twitter">
                        <svg viewBox="0 0 24 24">
                            <path d="M23 3a10.9 10.9 0 01-3.14 1.53 4.48 4.48 0 00-7.86 3v1A10.66 10.66 0 013 4s-4 9 5 13a11.64 11.64 0 01-7 2c9 5 20 0 20-11.5a4.5 4.5 0 00-.08-.83A7.72 7.72 0 0023 3z"></path>
                        </svg>
                    </a>
                    <a href="https://www.linkedin.com/sharing/share-offsite/?url=<?php echo esc_attr($share_url); ?>" target="_blank" class="me-social-icon linkedin" title="LinkedIn">
                        <svg viewBox="0 0 24 24">
                            <path d="M16 8a6 6 0 016 6v7h-4v-7a2 2 0 00-2-2 2 2 0 00-2 2v7h-4v-7a6 6 0 016-6zM2 9h4v12H2z"></path>
                            <circle cx="4" cy="4" r="2"></circle>
                        </svg>
                    </a>
                    <a href="https://pinterest.com/pin/create/button/?url=<?php echo esc_attr($share_url); ?>&media=<?php echo esc_attr(urlencode($poster)); ?>&description=<?php echo esc_attr($share_title); ?>" target="_blank" class="me-social-icon pinterest" title="Pinterest">
                        <svg viewBox="0 0 24 24">
                            <path d="M8 22s1.16-3.72 1.62-5.4A8.75 8.75 0 0112 18a6 6 0 10-6-6 4 4 0 014 4 1 1 0 01-1 1A1.75 1.75 0 017 15a8.75 8.75 0 115 5c-1.35 0-2.61-.41-3.66-1.11z"></path>
                        </svg>
                    </a>
                    <a href="https://api.whatsapp.com/send?text=<?php echo esc_attr($share_title . '%20' . $share_url); ?>" target="_blank" class="me-social-icon whatsapp" title="WhatsApp">
                        <svg viewBox="0 0 24 24">
                            <path d="M21 11.5a8.38 8.38 0 01-.9 3.8 8.5 8.5 0 11-7.6-11.4 8.38 8.38 0 013.8.9L21 3.5l-1.5 4.5z"></path>
                        </svg>
                    </a>
                    <a href="https://t.me/share/url?url=<?php echo esc_attr($share_url); ?>&text=<?php echo esc_attr($share_title); ?>" target="_blank" class="me-social-icon telegram" title="Telegram">
                        <svg viewBox="0 0 24 24">
                            <path d="M22 2L11 13M22 2l-7 20-4-9-9-4 20-7z"></path>
                        </svg>
                    </a>
                </div>
            </div>
        </div>

        <?php if (is_user_logged_in()) : ?>
            <div id="movie-engine-playlist-modal" class="movie-engine-modal me-movie-playlist-modal" style="display: none;">
                <div class="movie-engine-modal-content">
                    <button class="movie-engine-close-modal">
                        <span class="fa-solid fa-xmark"></span>
                    </button>
                    <div class="movie-engine-modal-header">
                        <h3><?php esc_html_e('Save to playlist', 'cine-movie-engine'); ?></h3>
                    </div>
                    <div class="movie-engine-playlist-form">
                        <div class="movie-engine-input-group">
                            <input type="text" id="movie-engine-new-playlist-name" placeholder="<?php esc_html_e('Enter playlist name...', 'cine-movie-engine'); ?>" class="movie-engine-input">
                            <button id="movie-engine-create-playlist-btn" class="movie-engine-btn movie-engine-btn-primary">
                                <?php esc_html_e('CREATE', 'cine-movie-engine'); ?>
                            </button>
                        </div>
                    </div>
                    <div class="movie-engine-playlist-list" id="movie-engine-playlist-list">
                        <?php
                        $user_playlists = \MovieEngine\Frontend\Users\UserLibrary::get_playlists(get_current_user_id());
                        if (!empty($user_playlists)) : ?>
                            <ul>
                                <?php foreach ($user_playlists as $pl) :
                                    $items = isset($pl['items']) && is_array($pl['items']) ? $pl['items'] : [];
                                    $is_in = in_array($post_id, $items);
                                    $count = count($items);
                                ?>
                                    <li class="movie-engine-playlist-item <?php echo $is_in ? 'in-playlist' : ''; ?>" data-id="<?php echo esc_attr($pl['id']); ?>">
                                        <div class="me-playlist-left-icon">
                                            <span class="fa-solid fa-list-ul"></span>
                                        </div>
                                        <div class="me-playlist-info">
                                            <span class="movie-engine-playlist-name"><?php echo esc_html($pl['name']); ?></span>

                                            <span class="movie-engine-playlist-count">
                                                <?php
                                                printf(
                                                    /* translators: %d: number of videos */
                                                    esc_html(_n('%d video', '%d videos', $count, 'cine-movie-engine')),
                                                    absint($count)
                                                );
                                                ?>
                                            </span>

                                        </div>
                                        <div class="me-playlist-status-icon">
                                            <span class="fa-solid fa-check"></span>
                                            <span class="me-circle-placeholder"></span>
                                        </div>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        <?php else: ?>
                            <div class="movie-engine-no-playlists">
                                <p><?php esc_html_e('No playlists yet.', 'cine-movie-engine'); ?></p>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <div id="movie-engine-login-modal" class="movie-engine-modal me-hidden me-login-modal">
            <div class="movie-engine-modal-content movie-engine-login-modal-content">
                <span class="movie-engine-close-modal">&times;</span>

                <div class="me-login-modal-icon">
                    <span class="fa-solid fa-lock"></span>
                </div>

                <h3 class="me-login-modal-title"><?php esc_html_e('Login Required', 'cine-movie-engine'); ?></h3>
                <p class="me-login-modal-text"><?php esc_html_e('Please login to create playlists and like content.', 'cine-movie-engine'); ?></p>

                <div class="me-login-modal-actions">
                    <a href="<?php echo esc_url(wp_login_url(get_permalink())); ?>" class="movie-engine-btn me-btn-primary">
                        <?php esc_html_e('Login', 'cine-movie-engine'); ?>
                    </a>
                    <a href="<?php echo esc_url(wp_registration_url()); ?>" class="movie-engine-btn me-btn-secondary">
                        <?php esc_html_e('Register', 'cine-movie-engine'); ?>
                    </a>
                </div>
            </div>
        </div>
    <?php
    }

    /**
     * Renders share, playlist, and login modals on pages that have the hero shortcode
     * but are NOT singular movie/series/episode pages (where render_details_modal already handles it).
     */
    public function render_interaction_modals() {
        if (is_admin()) {
            return;
        }

        // Skip if render_details_modal already output these modals (singular pages)
        if (is_singular(['movie_engine_movie', 'movie_engine_series', 'movie_engine_episode'])) {
            return;
        }

        // Only render if the hero shortcode flagged that it needs them
        if (empty($GLOBALS['movie_engine_needs_interaction_modals'])) {
            return;
        }

        $post_id = get_the_ID();
        $poster  = get_the_post_thumbnail_url($post_id, 'large') ?: '';
    ?>

        <div id="movie-engine-share-modal" class="movie-engine-modal me-hidden me-movie-share-modal">
            <div class="movie-engine-modal-content me-share-modal-modern">
                <span class="movie-engine-close-modal">&times;</span>

                <div class="me-share-header">
                    <h3><?php
                        if (apply_filters('movie_engine_is_pro', false)) {
                            esc_html_e('Share Your Link & Earn', 'cine-movie-engine');
                        } else {
                            esc_html_e('Share Current Item', 'cine-movie-engine');
                        }
                        ?></h3>
                    <p><?php
                        if (apply_filters('movie_engine_is_pro', false)) {
                            esc_html_e('Copy your unique referral link and share it with your friends.', 'cine-movie-engine');
                        } else {
                            esc_html_e('Copy link and share it with your friends.', 'cine-movie-engine');
                        }
                        ?></p>
                </div>

                <div class="me-share-url-container">
                    <div class="me-share-input-wrapper">
                        <?php
                        $share_link = apply_filters('movie_engine_share_link', get_permalink($post_id), $post_id);
                        ?>
                        <input type="text" readonly value="<?php echo esc_attr($share_link); ?>" id="movie-engine-share-url-input">
                        <button id="movie-engine-copy-link-btn"><?php esc_html_e('Copy', 'cine-movie-engine'); ?></button>
                    </div>
                </div>

                <div class="me-social-share-grid">
                    <?php
                    $share_url = urlencode(get_permalink($post_id));
                    $share_title = urlencode(get_the_title($post_id));
                    ?>
                    <a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo esc_attr($share_url); ?>" target="_blank" class="me-social-icon facebook" title="Facebook">
                        <svg viewBox="0 0 24 24">
                            <path d="M18 2h-3a5 5 0 00-5 5v3H7v4h3v8h4v-8h3l1-4h-4V7a1 1 0 011-1h3z"></path>
                        </svg>
                    </a>
                    <a href="https://twitter.com/intent/tweet?url=<?php echo esc_attr($share_url); ?>&text=<?php echo esc_attr($share_title); ?>" target="_blank" class="me-social-icon twitter" title="Twitter">
                        <svg viewBox="0 0 24 24">
                            <path d="M23 3a10.9 10.9 0 01-3.14 1.53 4.48 4.48 0 00-7.86 3v1A10.66 10.66 0 013 4s-4 9 5 13a11.64 11.64 0 01-7 2c9 5 20 0 20-11.5a4.5 4.5 0 00-.08-.83A7.72 7.72 0 0023 3z"></path>
                        </svg>
                    </a>
                    <a href="https://api.whatsapp.com/send?text=<?php echo esc_attr($share_title . '%20' . $share_url); ?>" target="_blank" class="me-social-icon whatsapp" title="WhatsApp">
                        <svg viewBox="0 0 24 24">
                            <path d="M21 11.5a8.38 8.38 0 01-.9 3.8 8.5 8.5 0 11-7.6-11.4 8.38 8.38 0 013.8.9L21 3.5l-1.5 4.5z"></path>
                        </svg>
                    </a>
                    <a href="https://t.me/share/url?url=<?php echo esc_attr($share_url); ?>&text=<?php echo esc_attr($share_title); ?>" target="_blank" class="me-social-icon telegram" title="Telegram">
                        <svg viewBox="0 0 24 24">
                            <path d="M22 2L11 13M22 2l-7 20-4-9-9-4 20-7z"></path>
                        </svg>
                    </a>
                </div>
            </div>
        </div>

        <?php if (is_user_logged_in()) : ?>
            <div id="movie-engine-playlist-modal" class="movie-engine-modal me-movie-playlist-modal" style="display: none;">
                <div class="movie-engine-modal-content">
                    <button class="movie-engine-close-modal">
                        <span class="fa-solid fa-xmark"></span>
                    </button>
                    <div class="movie-engine-modal-header">
                        <h3><?php esc_html_e('Save to playlist', 'cine-movie-engine'); ?></h3>
                    </div>
                    <div class="movie-engine-playlist-form">
                        <div class="movie-engine-input-group">
                            <input type="text" id="movie-engine-new-playlist-name" placeholder="<?php esc_html_e('Enter playlist name...', 'cine-movie-engine'); ?>" class="movie-engine-input">
                            <button id="movie-engine-create-playlist-btn" class="movie-engine-btn movie-engine-btn-primary">
                                <?php esc_html_e('CREATE', 'cine-movie-engine'); ?>
                            </button>
                        </div>
                    </div>
                    <div class="movie-engine-playlist-list" id="movie-engine-playlist-list">
                        <?php
                        $user_playlists = \MovieEngine\Frontend\Users\UserLibrary::get_playlists(get_current_user_id());
                        if (!empty($user_playlists)) : ?>
                            <ul>
                                <?php foreach ($user_playlists as $pl) :
                                    $items = isset($pl['items']) && is_array($pl['items']) ? $pl['items'] : [];
                                    $count = count($items);
                                ?>
                                    <li class="movie-engine-playlist-item" data-id="<?php echo esc_attr($pl['id']); ?>">
                                        <div class="me-playlist-left-icon">
                                            <span class="fa-solid fa-list-ul"></span>
                                        </div>
                                        <div class="me-playlist-info">
                                            <span class="movie-engine-playlist-name"><?php echo esc_html($pl['name']); ?></span>
                                            <span class="movie-engine-playlist-count">
                                                <?php
                                                printf(
                                                    /* translators: %d: number of videos */
                                                    esc_html(_n('%d video', '%d videos', $count, 'cine-movie-engine')),
                                                    absint($count)
                                                );
                                                ?>
                                            </span>
                                        </div>
                                        <div class="me-playlist-status-icon">
                                            <span class="fa-solid fa-check"></span>
                                            <span class="me-circle-placeholder"></span>
                                        </div>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        <?php else: ?>
                            <div class="movie-engine-no-playlists">
                                <p><?php esc_html_e('No playlists yet.', 'cine-movie-engine'); ?></p>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <div id="movie-engine-login-modal" class="movie-engine-modal me-hidden me-login-modal">
            <div class="movie-engine-modal-content movie-engine-login-modal-content">
                <span class="movie-engine-close-modal">&times;</span>

                <div class="me-login-modal-icon">
                    <span class="fa-solid fa-lock"></span>
                </div>

                <h3 class="me-login-modal-title"><?php esc_html_e('Login Required', 'cine-movie-engine'); ?></h3>
                <p class="me-login-modal-text"><?php esc_html_e('Please login to create playlists and like content.', 'cine-movie-engine'); ?></p>

                <div class="me-login-modal-actions">
                    <a href="<?php echo esc_url(wp_login_url(get_permalink())); ?>" class="movie-engine-btn me-btn-primary">
                        <?php esc_html_e('Login', 'cine-movie-engine'); ?>
                    </a>
                    <a href="<?php echo esc_url(wp_registration_url()); ?>" class="movie-engine-btn me-btn-secondary">
                        <?php esc_html_e('Register', 'cine-movie-engine'); ?>
                    </a>
                </div>
            </div>
        </div>

    <?php
    }

    /**
     * Adds dynamic classes to the <body> tag based on Movie Engine settings and page context.
     *
     * @param array $classes Existing body classes.
     * @return array Modified body classes.
     */
    public function add_dynamic_body_classes($classes) {
        if (is_admin()) {
            return $classes;
        }

        $options = get_option('movie_engine_design_settings', []);
        $current_id = get_queried_object_id();

        // Map settings keys to body classes
        $page_map = [
            'movie_archive_page'  => 'me-movie-archive-page',
            'series_archive_page' => 'me-series-archive-page',
            'person_archive_page' => 'me-person-archive-page',
            'dashboard_page'      => 'me-dashboard-page',
            'browse_page'         => 'me-browse-page',
            'tv_series_page'      => 'me-tv-series-page',
            'search_page'         => 'me-search-results-page',
            'login_page'          => 'me-login-page',
            'register_page'       => 'me-register-page',
        ];

        foreach ($page_map as $option_key => $class_name) {
            if (!empty($options[$option_key]) && (int) $options[$option_key] === (int) $current_id) {
                $classes[] = $class_name;
            }
        }

        // Add class for taxonomy pages
        if (is_tax(['movie_engine_genre', 'movie_engine_actor', 'movie_engine_director', 'movie_engine_writer', 'movie_engine_network', 'movie_engine_country', 'movie_engine_year_tax', 'movie_engine_quality'])) {
            $classes[] = 'me-taxonomy-page';
        }

        // Standard archive check
        if (is_post_type_archive(['movie_engine_movie', 'movie_engine_series', 'movie_engine_person'])) {
            $classes[] = 'me-archive-page';
        }

        return array_unique($classes);
    }
}
