<?php
declare(strict_types=1);
$notifications = is_array($notifications ?? null) ? $notifications : [];
$preferences = is_array($preferences ?? null) ? $preferences : [];
?>
<section class="content-section notifications-section">
    <div class="container narrow-container">
        <p class="eyebrow"><?= quaeriva_t($translator, 'notifications.eyebrow') ?></p>
        <h1><?= quaeriva_t($translator, 'notifications.title') ?></h1>
        <p class="section-lede"><?= quaeriva_t($translator, 'notifications.intro') ?></p>

        <section class="notification-preferences" aria-labelledby="notification-preferences-heading">
            <h2 id="notification-preferences-heading"><?= quaeriva_t($translator, 'notifications.preferences') ?></h2>
            <form class="question-form" method="post" action="<?= quaeriva_escape(quaeriva_url('notifications/preferences')) ?>">
                <input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken ?? '') ?>">
                <?php foreach (['new_answer', 'new_comment', 'answer_accepted', 'followed_question'] as $event): $pref = $preferences[$event] ?? ['in_app' => true, 'email' => false]; ?>
                    <fieldset class="notification-preference-row">
                        <legend><?= quaeriva_t($translator, 'notifications.events.' . $event) ?></legend>
                        <label class="check-row"><input type="checkbox" name="<?= quaeriva_escape($event) ?>_in_app" value="1"<?= !empty($pref['in_app']) ? ' checked' : '' ?>> <span><?= quaeriva_t($translator, 'notifications.in_app') ?></span></label>
                        <label class="check-row"><input type="checkbox" name="<?= quaeriva_escape($event) ?>_email" value="1"<?= !empty($pref['email']) ? ' checked' : '' ?><?= (($site['mail_enabled'] ?? false) === true) ? '' : ' disabled' ?>> <span><?= quaeriva_t($translator, 'notifications.email') ?></span></label>
                    </fieldset>
                <?php endforeach; ?>
                <button class="button button-small" type="submit"><?= quaeriva_t($translator, 'common.save') ?></button>
            </form>
        </section>

        <section class="notification-inbox" aria-labelledby="notification-inbox-heading">
            <div class="section-heading compact"><h2 id="notification-inbox-heading"><?= quaeriva_t($translator, 'notifications.inbox') ?></h2></div>
            <?php if ($notifications === []): ?>
                <div class="empty-state empty-state-small"><p><?= quaeriva_t($translator, 'notifications.empty') ?></p></div>
            <?php else: ?>
                <div class="notification-list">
                    <?php foreach ($notifications as $item): $isUnread = empty($item['read_at']); ?>
                        <article class="notification-item<?= $isUnread ? ' notification-unread' : '' ?>">
                            <div>
                                <h3><?= quaeriva_escape((string) ($item['title'] ?? '')) ?></h3>
                                <?php if (trim((string) ($item['body'] ?? '')) !== ''): ?><p dir="auto"><?= quaeriva_escape((string) $item['body']) ?></p><?php endif; ?>
                                <small><bdi><?= quaeriva_escape(quaeriva_relative_time((string) ($item['created_at'] ?? ''), $translator)) ?></bdi></small>
                            </div>
                            <div class="notification-actions">
                                <?php if (trim((string) ($item['url'] ?? '')) !== ''): ?><a class="button button-small button-secondary" href="<?= quaeriva_escape((string) $item['url']) ?>"><?= quaeriva_t($translator, 'notifications.open') ?></a><?php endif; ?>
                                <?php if ($isUnread): ?>
                                    <form method="post" action="<?= quaeriva_escape(quaeriva_url('notifications/read')) ?>">
                                        <input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken ?? '') ?>">
                                        <input type="hidden" name="notification_id" value="<?= (int) ($item['id'] ?? 0) ?>">
                                        <button class="button button-small button-ghost" type="submit"><?= quaeriva_t($translator, 'notifications.mark_read') ?></button>
                                    </form>
                                <?php endif; ?>
                            </div>
                        </article>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </section>
    </div>
</section>
