<?php
require __DIR__ . '/app/bootstrap.php';
require_auth();
$user=current_user();
$id=(int)request('id',0);
$q=db()->prepare('SELECT * FROM notifications WHERE id=? AND (user_id=? OR user_id IS NULL) LIMIT 1');
$q->execute([$id,$user['id']]);
$n=$q->fetch();
if(!$n){redirect('dashboard.php?page=notifications');}
db()->prepare('UPDATE notifications SET read_at=COALESCE(read_at,?) WHERE id=?')->execute([now(),$id]);
$link=trim((string)($n['link_url']??''));
if($link==='' || !preg_match('~^(dashboard\.php\?page=[a-z_]+(?:&[a-z0-9_=&%-]+)?|marketplace\.php(?:\?.*)?)$~i',$link))$link=notification_default_link($n);
redirect($link);
