<?php

/**
 * The template for displaying episode navigation
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/single-episode/navigation.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 = get_the_ID();
$movie_engine_series_id = get_post_meta($movie_engine_post_id, '_movie_engine_series_id', true);
$movie_engine_season_num = get_post_meta($movie_engine_post_id, '_movie_engine_season_number', true);
$movie_engine_episode_num = get_post_meta($movie_engine_post_id, '_movie_engine_episode_number', true);

$movie_engine_player_nav_settings = get_option('movie_engine_player_settings', []);
$movie_engine_show_nav = isset($movie_engine_player_nav_settings['movie_engine_show_nav_below_player']) ? $movie_engine_player_nav_settings['movie_engine_show_nav_below_player'] : (isset($movie_engine_player_nav_settings['show_nav_below_player']) ? $movie_engine_player_nav_settings['show_nav_below_player'] : 1);

if ($movie_engine_show_nav) :
?>
    <div class="movie-engine-episode-nav">
        <?php
        $movie_engine_prev_ep = get_posts([
            'post_type' => 'movie_engine_episode',
            // phpcs:ignore WordPress.DB.SlowDBQuery
            'meta_query' => [
                'relation' => 'AND',
                ['key' => '_movie_engine_series_id', 'value' => $movie_engine_series_id],
                ['key' => '_movie_engine_season_number', 'value' => $movie_engine_season_num],
                ['key' => '_movie_engine_episode_number', 'value' => $movie_engine_episode_num - 1]
            ],
            'posts_per_page' => 1,
            'fields' => 'ids'
        ]);

        if ($movie_engine_prev_ep) {
            echo '<a href="' . esc_url(get_permalink($movie_engine_prev_ep[0])) . '" class="movie-engine-btn me-btn-nav me-prev-ep"><span class="fa-solid fa-angle-left"></span> ' . esc_html__('Previous Episode', 'cine-movie-engine') . '</a>';
        } else {
            echo '<span class="movie-engine-btn me-btn-nav me-disabled"><span class="fa-solid fa-angle-left"></span> ' . esc_html__('Previous Episode', 'cine-movie-engine') . '</span>';
        }

        $movie_engine_next_ep = get_posts([
            'post_type' => 'movie_engine_episode',
            // phpcs:ignore WordPress.DB.SlowDBQuery
            'meta_query' => [
                'relation' => 'AND',
                ['key' => '_movie_engine_series_id', 'value' => $movie_engine_series_id],
                ['key' => '_movie_engine_season_number', 'value' => $movie_engine_season_num],
                ['key' => '_movie_engine_episode_number', 'value' => $movie_engine_episode_num + 1]
            ],
            'posts_per_page' => 1,
            'fields' => 'ids'
        ]);

        if ($movie_engine_next_ep) {
            echo '<a href="' . esc_url(get_permalink($movie_engine_next_ep[0])) . '" class="movie-engine-btn me-btn-nav me-next-ep">' . esc_html__('Next Episode', 'cine-movie-engine') . ' <span class="fa-solid fa-angle-right"></span></a>';
        } else {
            echo '<span class="movie-engine-btn me-btn-nav me-disabled">' . esc_html__('Next Episode', 'cine-movie-engine') . ' <span class="fa-solid fa-angle-right"></span></span>';
        }
        ?>
    </div>
<?php endif; ?>