<?php
use BLR\Backline\Modules\Notifications\Module as NotificationsModule;

if (!defined('ABSPATH')) {
    exit;
}

$user_id = get_current_user_id();
NotificationsModule::sync_user_notifications($user_id);
$counts = NotificationsModule::counts_for_user($user_id, false);
$notifications = NotificationsModule::list_for_user($user_id, [
    'limit' => 100,
    'include_read' => true,
], false);
?>
<div class="container-fluid p-0">
    <div class="d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-3 mb-4">
        <div>
            <h1 class="h3 mb-0"><?php echo esc_html__('Notifications', 'blr-backline'); ?></h1>
        </div>

        <div class="d-flex flex-wrap gap-2">
            <form method="post" action="<?php echo esc_url(NotificationsModule::page_url()); ?>">
                <?php wp_nonce_field(NotificationsModule::action_nonce()); ?>
                <input type="hidden" name="blr_ba_action" value="blr_ba_notification_mark_all_read">
                <button type="submit" class="btn btn-outline-secondary"><?php echo esc_html__('Mark all as read', 'blr-backline'); ?></button>
            </form>
            <form method="post" action="<?php echo esc_url(NotificationsModule::page_url()); ?>">
                <?php wp_nonce_field(NotificationsModule::action_nonce()); ?>
                <input type="hidden" name="blr_ba_action" value="blr_ba_notification_dismiss_read">
                <button type="submit" class="btn btn-outline-secondary"><?php echo esc_html__('Dismiss read', 'blr-backline'); ?></button>
            </form>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-12 col-md-4">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body">
                    <div class="small text-uppercase text-muted fw-semibold mb-2"><?php echo esc_html__('Unread', 'blr-backline'); ?></div>
                    <div class="display-6 lh-1 mb-0"><?php echo esc_html(number_format_i18n((int) $counts['unread'])); ?></div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-4">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body">
                    <div class="small text-uppercase text-muted fw-semibold mb-2"><?php echo esc_html__('Open', 'blr-backline'); ?></div>
                    <div class="display-6 lh-1 mb-0"><?php echo esc_html(number_format_i18n((int) $counts['total'])); ?></div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-4">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body">
                    <div class="small text-uppercase text-muted fw-semibold mb-2"><?php echo esc_html__('Read', 'blr-backline'); ?></div>
                    <div class="display-6 lh-1 mb-0"><?php echo esc_html(number_format_i18n((int) $counts['read'])); ?></div>
                </div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="card-header d-flex align-items-center justify-content-between">
            <h5 class="card-title mb-0"><?php echo esc_html__('Active notifications', 'blr-backline'); ?></h5>
            <span class="badge bg-secondary"><?php echo esc_html(number_format_i18n(count($notifications))); ?></span>
        </div>
        <div class="card-body p-0">
            <?php if (empty($notifications)) : ?>
                <div class="p-4 text-muted"><?php echo esc_html__('No active notifications right now.', 'blr-backline'); ?></div>
            <?php else : ?>
                <div class="list-group list-group-flush">
                    <?php foreach ($notifications as $notification) : ?>
                        <?php
                        $is_unread = empty($notification->is_read);
                        $priority_class = NotificationsModule::priority_badge_class((int) $notification->priority);
                        ?>
                        <div class="list-group-item px-3 py-3<?php echo esc_attr($is_unread ? ' bg-body-tertiary' : ''); ?>">
                            <div class="d-flex align-items-start justify-content-between gap-3">
                                <div class="d-flex align-items-start gap-3 flex-grow-1">
                                    <div class="pt-1">
                                        <i class="align-middle text-muted mt-4" data-feather="<?php echo esc_attr((string) $notification->icon); ?>"></i>
                                    </div>
                                    <div class="flex-grow-1">
                                        <div class="d-flex flex-wrap align-items-center gap-2 mb-1">
                                            <a class="fw-semibold text-decoration-none" href="<?php echo esc_url(NotificationsModule::link_url($notification)); ?>">
                                                <?php echo esc_html((string) $notification->title); ?>
                                            </a>
                                            <?php if ($is_unread) : ?>
                                                <span class="badge bg-primary"><?php echo esc_html__('Unread', 'blr-backline'); ?></span>
                                            <?php endif; ?>
                                            <span class="badge bg-<?php echo esc_attr($priority_class); ?>">
                                                <?php echo esc_html(NotificationsModule::type_label((string) $notification->type)); ?>
                                            </span>
                                        </div>
                                        <div class="text-muted small mb-2"><?php echo esc_html((string) $notification->message); ?></div>
                                        <div class="small text-muted"><?php echo esc_html(NotificationsModule::relative_time((string) ($notification->event_time ?: $notification->updated_at))); ?></div>
                                    </div>
                                </div>
                                <div class="d-flex flex-column flex-sm-row gap-2">
                                    <?php if ($is_unread) : ?>
                                        <form method="post" action="<?php echo esc_url(NotificationsModule::page_url()); ?>">
                                            <?php wp_nonce_field(NotificationsModule::action_nonce()); ?>
                                            <input type="hidden" name="blr_ba_action" value="blr_ba_notification_mark_read">
                                            <input type="hidden" name="blr_notification_id" value="<?php echo esc_attr((string) $notification->id); ?>">
                                            <button type="submit" class="btn btn-sm btn-outline-secondary"><?php echo esc_html__('Mark read', 'blr-backline'); ?></button>
                                        </form>
                                    <?php endif; ?>
                                    <form method="post" action="<?php echo esc_url(NotificationsModule::page_url()); ?>">
                                        <?php wp_nonce_field(NotificationsModule::action_nonce()); ?>
                                        <input type="hidden" name="blr_ba_action" value="blr_ba_notification_dismiss">
                                        <input type="hidden" name="blr_notification_id" value="<?php echo esc_attr((string) $notification->id); ?>">
                                        <button type="submit" class="btn btn-sm btn-outline-danger mt-4"><?php echo esc_html__('Dismiss', 'blr-backline'); ?></button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>
