<?php
declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Core\Database;

final class AuthorNotificationsController extends Controller
{
    public function index(): void
    {
        require_login();
        $db=Database::getInstance();
        $uid=(int)auth_id();

        $st=$db->prepare("SELECT * FROM notifications WHERE user_id=:u ORDER BY id DESC LIMIT 200");
        $st->execute([':u'=>$uid]);
        $rows=$st->fetchAll()?:[];

        $this->view('author/notifications',['rows'=>$rows]);
    }

    public function read(): void
    {
        require_login();
        csrf_check();
        $db=Database::getInstance();
        $uid=(int)auth_id();
        $id=(int)($_POST['id']??0);
        $db->prepare("UPDATE notifications SET is_read=1 WHERE id=:id AND user_id=:u")->execute([':id'=>$id,':u'=>$uid]);
        header('Location: '.base_path('author/notifications')); exit;
    }

    public function readAll(): void
    {
        require_login();
        csrf_check();
        $db=Database::getInstance();
        $uid=(int)auth_id();
        $db->prepare("UPDATE notifications SET is_read=1 WHERE user_id=:u")->execute([':u'=>$uid]);
        header('Location: '.base_path('author/notifications')); exit;
    }
}
