<?php

/**
 * The template for displaying taxonomy archive pages (genres, countries, cast, etc.)
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/taxonomy.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
}

get_header();

$movie_engine_term = get_queried_object();
$movie_engine_taxonomy = get_taxonomy($movie_engine_term->taxonomy);
$movie_engine_taxonomy_label = $movie_engine_taxonomy->labels->singular_name;

/**
 * Hooks for before taxonomy archive
 */
do_action('movie_engine_before_taxonomy_archive', $movie_engine_term, $movie_engine_taxonomy);
?>

<?php
$movie_engine_hero_bg_image = '';
$movie_engine_latest_posts = get_posts([
    'post_type'      => ['movie_engine_movie', 'movie_engine_series'],
    'posts_per_page' => 1,
    // phpcs:ignore WordPress.DB.SlowDBQuery
    'tax_query'      => [
        [
            'taxonomy' => $movie_engine_term->taxonomy,
            'field'    => 'term_id',
            'terms'    => $movie_engine_term->term_id,
        ],
    ],
]);

if (!empty($movie_engine_latest_posts)) {
    $movie_engine_hero_bg_image = get_the_post_thumbnail_url($movie_engine_latest_posts[0]->ID, 'full');
}

$movie_engine_ajax_data = '';
if ($movie_engine_term->taxonomy === 'movie_engine_genre') $movie_engine_ajax_data = 'data-me_genre="' . esc_attr($movie_engine_term->slug) . '"';
if ($movie_engine_term->taxonomy === 'movie_engine_year') $movie_engine_ajax_data = 'data-me_year="' . esc_attr($movie_engine_term->slug) . '"';
if ($movie_engine_term->taxonomy === 'movie_engine_tag') $movie_engine_ajax_data = 'data-me_tag="' . esc_attr($movie_engine_term->slug) . '"';
if ($movie_engine_term->taxonomy === 'movie_engine_language') $movie_engine_ajax_data = 'data-me_language="' . esc_attr($movie_engine_term->slug) . '"';
if ($movie_engine_term->taxonomy === 'movie_engine_quality' || $movie_engine_term->taxonomy === 'movie_engine_tv_quality') $movie_engine_ajax_data = 'data-me_quality="' . esc_attr($movie_engine_term->slug) . '"';
?>

<div class="movie-engine-archive-wrapper movie-engine-plugin movie-engine-ajax-container" data-action="movie_engine_filter_movies" <?php echo wp_kses($movie_engine_ajax_data ?? '', []); ?>>
    <section class="me-taxonomy-hero <?php echo esc_attr($movie_engine_hero_bg_image ? 'has-bg' : ''); ?>">
        <?php if ($movie_engine_hero_bg_image) : ?>
            <div class="me-hero-bg" style="background-image: url('<?php echo esc_url($movie_engine_hero_bg_image); ?>');"></div>
        <?php endif; ?>
        <div class="me-hero-overlay"></div>

        <div class="movie-engine-container">
            <div class="me-hero-content">
                <?php
                /**
                 * Hooks before archive header info
                 */
                do_action('movie_engine_before_archive_header', $movie_engine_term, $movie_engine_taxonomy);
                ?>
                <header class="me-hero-header">
                    <span class="me-hero-subtitle"><?php echo esc_html($movie_engine_taxonomy_label); ?></span>
                    <h1 class="me-hero-title"><?php echo esc_html($movie_engine_term->name); ?></h1>

                    <div class="me-hero-meta">
                        <span class="me-hero-count">
                            <span class="fa-solid fa-video" aria-hidden="true"></span>
                            <?php printf(
                                /* translators: %s: total count of titles formatted */
                                esc_html(_n('%s Title', '%s Titles', $movie_engine_term->count, 'cine-movie-engine')),
                                esc_html(number_format_i18n($movie_engine_term->count))
                            ); ?>
                        </span>
                        <?php if (!empty($movie_engine_term->description)) : ?>
                            <div class="me-hero-description">
                                <?php echo \MovieEngine\Utils\Helpers::kses_post($movie_engine_term->description); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </header>
                <?php
                /**
                 * Hooks after archive header info
                 */
                do_action('movie_engine_after_archive_header', $movie_engine_term, $movie_engine_taxonomy);
                ?>
            </div>
        </div>
    </section>

    <div class="movie-engine-taxonomy-wrapper">
        <div class="movie-engine-container">
            <div class="me-sticky-filter-wrapper">
                <?php \MovieEngine\Utils\TemplateLoader::get_template_part('archive/filter-bar'); ?>
            </div>
        </div>

        <div class="me-archive-results-wrapper">
            <div class="movie-engine-container-fluid">
                <?php if (have_posts()) :
                    $movie_engine_design_settings = get_option('movie_engine_design_settings', []);
                    $movie_engine_pagination_type = isset($movie_engine_design_settings['pagination_type']) ? $movie_engine_design_settings['pagination_type'] : 'numbers';
                    $movie_engine_max_pages = $wp_query->max_num_pages;
                    $movie_engine_paged = max(1, get_query_var('paged'));

                    $movie_engine_cols = $movie_engine_design_settings['archive_columns_desktop'] ?? 4;
                    $movie_engine_cols_laptop = $movie_engine_design_settings['archive_columns_laptop'] ?? 4;
                    $movie_engine_cols_tablet = $movie_engine_design_settings['archive_columns_tablet'] ?? 3;
                    $movie_engine_cols_mobile = $movie_engine_design_settings['archive_columns_mobile'] ?? 2;
                    $movie_engine_grid_style = "style='--grid-columns: " . esc_attr($movie_engine_cols) . "; --grid-columns-laptop: " . esc_attr($movie_engine_cols_laptop) . "; --grid-columns-tablet: " . esc_attr($movie_engine_cols_tablet) . "; --grid-columns-mobile: " . esc_attr($movie_engine_cols_mobile) . "; --me-grid-columns-desktop: " . esc_attr($movie_engine_cols) . "; --me-grid-columns-laptop: " . esc_attr($movie_engine_cols_laptop) . "; --me-grid-columns-tablet: " . esc_attr($movie_engine_cols_tablet) . "; --me-grid-columns-mobile: " . esc_attr($movie_engine_cols_mobile) . ";'";
                ?>
                    <section class="movie-engine-section">
                        <div class="movie-engine-items-grid movie-engine-movie-grid" <?php echo $movie_engine_grid_style; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 
                                                            ?>>
                        <?php
                        while (have_posts()) :
                            the_post();
                            \MovieEngine\Utils\TemplateLoader::get_template_part('loop/content-movie');
                        endwhile;
                        ?>
                        </div>
                    </section>

                    <nav class="movie-engine-pagination" data-type="<?php echo esc_attr($movie_engine_pagination_type); ?>" data-max-pages="<?php echo esc_attr($movie_engine_max_pages); ?>" aria-label="<?php esc_attr_e('Pagination', 'cine-movie-engine'); ?>">
                        <?php
                        if ($movie_engine_pagination_type === 'load_more' || $movie_engine_pagination_type === 'infinite') {
                            if ($movie_engine_paged < $movie_engine_max_pages) {
                                echo '<button class="movie-engine-load-more-btn me-btn me-btn-primary" data-paged="' . esc_attr($movie_engine_paged + 1) . '" aria-label="' . esc_attr__('Load more movies', 'cine-movie-engine') . '">' . esc_html__('Load More', 'cine-movie-engine') . '</button>';
                                if ($movie_engine_pagination_type === 'infinite') {
                                    echo '<div class="movie-engine-infinite-trigger" aria-hidden="true"></div>';
                                }
                            }
                        } else {
                            $pagination_links = paginate_links([
                                'prev_text' => '<span class="fa-solid fa-angle-left" aria-hidden="true"></span><span class="me-sr-only">' . esc_html__('Previous', 'cine-movie-engine') . '</span>',
                                'next_text' => '<span class="fa-solid fa-angle-right" aria-hidden="true"></span><span class="me-sr-only">' . esc_html__('Next', 'cine-movie-engine') . '</span>',
                            ]);
                            if ($pagination_links) {
                                $pagination_links = str_replace('class="prev page-numbers"', 'class="prev page-numbers" aria-label="' . esc_attr__('Previous Page', 'cine-movie-engine') . '"', $pagination_links);
                                $pagination_links = str_replace('class="next page-numbers"', 'class="next page-numbers" aria-label="' . esc_attr__('Next Page', 'cine-movie-engine') . '"', $pagination_links);
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML
                                echo \MovieEngine\Utils\Helpers::kses_post((string) $pagination_links);
                            }
                        }
                        ?>
                    </nav>
                <?php else : ?>
                    <div class="movie-engine-no-results">
                        <p><?php echo esc_html(sprintf(
                                /* translators: %s: taxonomy label (e.g. Genre) */
                                __('No content found for this %s.', 'cine-movie-engine'),
                                strtolower($movie_engine_taxonomy_label)
                            )); ?></p>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php
/**
 * Hooks for after taxonomy archive
 */
do_action('movie_engine_after_taxonomy_archive', $movie_engine_term, $movie_engine_taxonomy);

get_footer();
