<?php
$items = is_array($notifications ?? null) ? $notifications : [];
$pageData = is_array($pagination ?? null) ? $pagination : ['page' => 1, 'per_page' => 30, 'total' => 0];
$page = max(1, (int) ($pageData['page'] ?? 1));
$perPage = max(1, (int) ($pageData['per_page'] ?? 30));
$total = max(0, (int) ($pageData['total'] ?? 0));
$totalPages = max(1, (int) ceil($total / $perPage));
?>

<section class="card notifications-page">
  <div class="notifications-page__head">
    <div>
      <h2 class="section-title"><?= $e($t('notifications.title')) ?></h2>
      <p class="muted mt-8"><?= $e($t('notifications.description')) ?></p>
    </div>
    <?php if ((int) ($unreadCount ?? 0) > 0): ?>
      <form method="post" action="/notifications/mark-read">
        <input type="hidden" name="_token" value="<?= $e((string) ($csrfToken ?? '')) ?>">
        <button class="btn btn--secondary btn--sm" type="submit"><?= $e($t('notifications.actions.mark_all_read')) ?></button>
      </form>
    <?php endif; ?>
  </div>

  <div class="notifications-list mt-16">
    <?php if ($items === []): ?>
      <div class="muted"><?= $e($t('notifications.empty')) ?></div>
    <?php endif; ?>
    <?php foreach ($items as $notification): ?>
      <?php
        $id = (int) ($notification['id'] ?? 0);
        $targetUrl = trim((string) ($notification['target_url'] ?? ''));
        $isUnread = trim((string) ($notification['read_at'] ?? '')) === '';
      ?>
      <article class="notification-row<?= $isUnread ? ' notification-row--unread' : '' ?>">
        <div class="notification-row__main">
          <div class="notification-row__title"><?= $e((string) ($notification['title'] ?? '')) ?></div>
          <div class="notification-row__body"><?= $e((string) ($notification['body'] ?? '')) ?></div>
          <div class="notification-row__meta"><?= $e((string) ($notification['created_at'] ?? '')) ?></div>
        </div>
        <div class="notification-row__actions">
          <?php if ($targetUrl !== ''): ?>
            <a class="btn btn--secondary btn--sm" href="<?= $e($targetUrl) ?>"><?= $e($t('notifications.actions.open')) ?></a>
          <?php endif; ?>
          <?php if ($isUnread): ?>
            <form method="post" action="/notifications/mark-read">
              <input type="hidden" name="_token" value="<?= $e((string) ($csrfToken ?? '')) ?>">
              <input type="hidden" name="id" value="<?= $e((string) $id) ?>">
              <button class="btn btn--secondary btn--sm" type="submit"><?= $e($t('notifications.actions.mark_read')) ?></button>
            </form>
          <?php endif; ?>
        </div>
      </article>
    <?php endforeach; ?>
  </div>

  <?php if ($totalPages > 1): ?>
    <div class="pagination mt-16">
      <?php if ($page > 1): ?>
        <a class="btn btn--secondary btn--sm" href="/notifications?page=<?= $page - 1 ?>">&larr;</a>
      <?php endif; ?>
      <span class="muted"><?= $e((string) $page) ?>/<?= $e((string) $totalPages) ?></span>
      <?php if ($page < $totalPages): ?>
        <a class="btn btn--secondary btn--sm" href="/notifications?page=<?= $page + 1 ?>">&rarr;</a>
      <?php endif; ?>
    </div>
  <?php endif; ?>
</section>
