<?php

declare(strict_types=1);
namespace App\Controllers;
use App\Core\Auth; use App\Core\Controller; use App\Core\Csrf; use App\Core\Response; use App\Core\Session; use App\Services\NotificationService;
final class NotificationController extends Controller
{
    public function __construct(private ?NotificationService $service=null){$this->service??=new NotificationService();}
    public function index(): Response
    { $uid=Auth::id()??0; return $this->view('notifications/index',['pageTitle'=>'Notifications','pageDescription'=>'Your CRM alerts and recent activity.','notifications'=>$this->service->latest($uid),'unread'=>$this->service->unreadCount($uid),'csrf'=>Csrf::token(),'message'=>Session::pull('notification_message')]); }
    public function read(int $id): Response
    { Csrf::verify((string)($_POST['_csrf']??'')); $this->service->markRead($id,Auth::id()??0); $link=(string)($_POST['link']??''); return $link!==''?Response::redirect($link):Response::redirect('/crm/public/notifications'); }
    public function readAll(): Response
    { Csrf::verify((string)($_POST['_csrf']??'')); $this->service->markAllRead(Auth::id()??0); return Response::redirect('/crm/public/notifications'); }
}
