<?php

/**
 * The template for displaying search results
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/search.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();
?>

<div class="movie-engine-archive-wrapper movie-engine-plugin movie-engine-ajax-container" data-action="movie_engine_filter_movies" data-s="<?php echo esc_attr(get_search_query()); ?>">
    <div class="movie-engine-container">
        <header class="movie-engine-archive-header">
            <h1 class="movie-engine-archive-title">
                <span class="movie-engine-subtitle"><?php esc_html_e('Search Results for:', 'cine-movie-engine'); ?></span>
                <?php echo esc_html(get_search_query()); ?>
            </h1>
        </header>
        <?php \MovieEngine\Utils\TemplateLoader::get_template_part('archive/filter-bar'); ?>

        <?php
        $movie_engine_design = get_option('movie_engine_design_settings', []);
        $movie_engine_layout = $movie_engine_design['search_layout'] ?? 'grid';
        $movie_engine_highlight = !empty($movie_engine_design['search_highlight_query']);
        ?>

        <?php if (have_posts()) :
            $movie_engine_pagination_type = isset($movie_engine_design['pagination_type']) || !empty($movie_engine_design['pagination_type']) ? $movie_engine_design['pagination_type'] : 'numbers';
            $movie_engine_max_pages = $wp_query->max_num_pages;
            $movie_engine_paged = max(1, get_query_var('paged'));
            $movie_engine_search_query = (string)get_search_query();
        ?>
            <div class="movie-engine-movie-grid me-layout-<?php echo esc_attr($movie_engine_layout); ?>">
                <?php
                while (have_posts()) :
                    the_post();
                    \MovieEngine\Utils\TemplateLoader::get_template_part('loop/content-movie', '', [
                        'highlight_query' => $movie_engine_highlight ? $movie_engine_search_query : ''
                    ]);
                endwhile;
                ?>
            </div>

            <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 esc_html_e('No movies found matching your criteria.', 'cine-movie-engine'); ?></p>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php
get_footer();
