<?php

/**
 * The template for displaying notifications list
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/dashboard/notifications-list.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/dashboard
 * @version 1.0.0
 * @since 1.0.0
 */

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

$movie_engine_user_id = get_current_user_id();
\MovieEngine\Frontend\Users\Notifications::seed_if_empty($movie_engine_user_id);
// phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
$movie_engine_notif_tab = isset($_POST['tab']) ? sanitize_text_field(wp_unslash($_POST['tab'])) : (isset($_GET['notif_tab']) ? sanitize_text_field(wp_unslash($_GET['notif_tab'])) : 'unread');
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$movie_engine_notif_per_page = isset($_POST['limit']) ? intval(wp_unslash($_POST['limit'])) : 10;
// phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
$movie_engine_notif_current_page = isset($_POST['paged']) ? max(1, intval(wp_unslash($_POST['paged']))) : (isset($_GET['paged']) ? max(1, intval(wp_unslash($_GET['paged']))) : 1);

$movie_engine_notifs = \MovieEngine\Frontend\Users\Notifications::get($movie_engine_user_id, 0);

$movie_engine_notif_filtered = [];
foreach ($movie_engine_notifs as $movie_engine_n) {
    if (!isset($movie_engine_n['read'])) $movie_engine_n['read'] = false;
    $movie_engine_is_read = (bool)$movie_engine_n['read'];
    if ($movie_engine_notif_tab === 'unread') {
        if (!$movie_engine_is_read) {
            $movie_engine_notif_filtered[] = $movie_engine_n;
        }
    } else {
        if ($movie_engine_is_read) {
            $movie_engine_notif_filtered[] = $movie_engine_n;
        }
    }
}

$movie_engine_notif_total_items = count($movie_engine_notif_filtered);
$movie_engine_notif_total_pages = ceil($movie_engine_notif_total_items / $movie_engine_notif_per_page);
$movie_engine_notif_offset = ($movie_engine_notif_current_page - 1) * $movie_engine_notif_per_page;
$movie_engine_paged_notifs = array_slice($movie_engine_notif_filtered, $movie_engine_notif_offset, $movie_engine_notif_per_page);
$movie_engine_has_unread = false;

foreach ($movie_engine_notifs as $movie_engine_n) {
    if (empty($movie_engine_n['read'])) {
        $movie_engine_has_unread = true;
        break;
    }
}
?>

<div class="movie-engine-notifications-list-inner">
    <?php if ($movie_engine_notif_tab === 'unread' && $movie_engine_has_unread) : ?>
        <div class="movie-engine-notif-actions-bar">
            <button class="movie-engine-btn me-btn-sm me-mark-all-read movie-engine-mark-all-read" aria-label="<?php esc_attr_e('Mark all as read', 'cine-movie-engine'); ?>">
                <i class="fa-solid fa-check-double" aria-hidden="true"></i>
                <?php esc_html_e('Mark all as read', 'cine-movie-engine'); ?>
            </button>
        </div>
    <?php endif; ?>
    <?php if (!empty($movie_engine_paged_notifs)) : foreach ($movie_engine_paged_notifs as $movie_engine_n) : ?>
            <div class="movie-engine-notif-item-premium me-mb-3 <?php echo esc_attr($movie_engine_n['read'] ? 'read' : 'unread'); ?> <?php echo esc_attr($movie_engine_n['type']); ?>" data-id="<?php echo esc_attr($movie_engine_n['id']); ?>">
                <div class="notif-media">
                    <div class="notif-icon-circle">
                        <?php if ($movie_engine_n['type'] === 'security') : ?>
                            <i class="fa-solid fa-shield-halved" aria-hidden="true"></i>
                        <?php elseif ($movie_engine_n['type'] === 'success') : ?>
                            <i class="fa-solid fa-check" aria-hidden="true"></i>
                        <?php else : ?>
                            <i class="fa-solid fa-bell" aria-hidden="true"></i>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="notif-content">
                    <p class="notif-text"><?php echo esc_html($movie_engine_n['message']); ?></p>
                    <span class="notif-time"><?php echo esc_html(human_time_diff($movie_engine_n['date'], time())); ?> <?php esc_html_e('ago', 'cine-movie-engine'); ?></span>
                </div>
                <div class="notif-actions">
                    <?php if (!$movie_engine_n['read']) : ?>
                        <button class="movie-engine-btn-mark-read" title="<?php esc_html_e('Mark as read', 'cine-movie-engine'); ?>" aria-label="<?php esc_attr_e('Mark as read', 'cine-movie-engine'); ?>">
                            <i class="fa-solid fa-eye" aria-hidden="true"></i>
                        </button>
                    <?php endif; ?>
                    <button class="movie-engine-btn-delete-notif" title="<?php esc_html_e('Delete', 'cine-movie-engine'); ?>" aria-label="<?php esc_attr_e('Delete notification', 'cine-movie-engine'); ?>">
                        <i class="fa-regular fa-trash-can" aria-hidden="true"></i>
                    </button>
                </div>
            </div>
        <?php endforeach; ?>

        <?php if ($movie_engine_notif_total_pages > 1) : ?>
            <nav class="movie-engine-pagination" aria-label="<?php esc_attr_e('Notifications pagination', 'cine-movie-engine'); ?>">
                <?php
                // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML, pagination params are int/controlled
                echo \MovieEngine\Utils\Helpers::kses_post(paginate_links(array(
                    'base'      => add_query_arg('paged', '%#%'),
                    'format'    => '',
                    'prev_text' => '<i class="fa-solid fa-chevron-left" aria-hidden="true"></i><span class="me-sr-only">' . esc_html__('Previous', 'cine-movie-engine') . '</span>',
                    'next_text' => '<i class="fa-solid fa-chevron-right" aria-hidden="true"></i><span class="me-sr-only">' . esc_html__('Next', 'cine-movie-engine') . '</span>',
                    'total'     => $movie_engine_notif_total_pages,
                    'current'   => $movie_engine_notif_current_page,
                    'add_args'  => ['notif_tab' => $movie_engine_notif_tab]
                )));
                // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
                ?>
            </nav>
        <?php endif; ?>

    <?php else : ?>
        <div class="movie-engine-empty-state">
            <i class="fa-solid fa-bell-slash" aria-hidden="true"></i>
            <p><?php echo ($movie_engine_notif_tab === 'unread') ? esc_html__('You have no unread notifications.', 'cine-movie-engine') : esc_html__('No archived notifications.', 'cine-movie-engine'); ?></p>
        </div>
    <?php endif; ?>
</div>