<?php
declare(strict_types=1);

namespace App\Controllers;

use App\Core\Auth;
use App\Core\Request;
use App\Models\Notification;

final class NotificationController extends BaseController
{
    public function open(Request $request): void
    {
        $uid = (int) Auth::instance()->id();
        $id = (int) $request->param('id');
        $row = Notification::findOwned($id, $uid);
        if ($row) {
            Notification::markRead($id, $uid);
            $path = trim((string) ($row['url'] ?? ''), '/');
            redirect($path !== '' ? $path : 'dashboard');
        }
        redirect('dashboard');
    }

    public function read(Request $request): void
    {
        $this->verifyCsrf($request);
        Notification::markRead((int) $request->param('id'), (int) Auth::instance()->id());
        json_out(['ok' => true]);
    }
}
