<?php

namespace App\Controllers;

use App\Models\NotificationModel;

/**
 * Full notification history — previously only reachable a handful at a
 * time via the topbar dropdown (see layouts/main.php), with no way to
 * see anything already marked read or older than "currently unread".
 * Same table/model as the dropdown, just a real list page over it.
 */
class NotificationController extends BaseController
{
    public function index()
    {
        $notifications = new NotificationModel();
        $tab = $this->request->getGet('tab') === 'unread' ? 'unread' : 'all';

        $builder = $tab === 'unread' ? $notifications->where('is_read', 0) : $notifications;

        $results = $builder->orderBy('created_at', 'desc')->paginate(20);

        return view('notifications/index', [
            'pageTitle'    => 'Notifications',
            'pageSubtitle' => 'Everything the system has flagged for staff attention.',
            'notifications' => $results,
            'pager'        => $notifications->pager,
            'tab'          => $tab,
            'unreadCount'  => (new NotificationModel())->unreadCount(),
        ]);
    }

    public function markRead()
    {
        (new NotificationModel())->markAllRead();

        return redirect()->back();
    }

    public function markOneRead(int $id)
    {
        (new NotificationModel())->markOneRead($id);

        return redirect()->back();
    }
}
