<?php

class NotificationController extends Controller
{
    private Notification $notifications;

    public function __construct()
    {
        $this->notifications = new Notification();
    }

    public function index(): void
    {
        Auth::requireLogin();
        $notifications = $this->notifications->forCurrentUser();
        $this->view('notifications/index', compact('notifications'));
    }

    public function read(): void
    {
        Auth::requireLogin();
        Csrf::verify();
        $id = (int) ($_POST['id'] ?? 0);
        $link = trim((string) ($_POST['link'] ?? ''));
        $this->notifications->markRead($id);
        $this->redirect($link && str_starts_with($link, '/') ? $link : '/notifications');
    }

    public function readAll(): void
    {
        Auth::requireLogin();
        Csrf::verify();
        $this->notifications->markAllRead();
        $this->redirect('/notifications');
    }
}
