<?php

namespace App\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Core\Session;
use App\Models\Notification;

class NotificationController extends Controller
{
    private Notification $model;

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

    public function index(Request $request): Response
    {
        $notifications = $this->model->getUnread(Session::get('user_id'));
        $this->model->markAllRead(Session::get('user_id'));
        return $this->render('notifications.index', compact('notifications'));
    }

    public function getUnread(Request $request): Response
    {
        $userId = Session::get('user_id');
        $notifications = $this->model->getUnread($userId);
        $count = $this->model->countUnread($userId);
        return Response::json(['count' => $count, 'notifications' => $notifications]);
    }

    public function markAsRead(Request $request, int $id): Response
    {
        $this->model->markAsRead($id);
        return Response::json(['ok' => true]);
    }

    public function markAllRead(Request $request): Response
    {
        $this->model->markAllRead(Session::get('user_id'));
        return Response::json(['ok' => true]);
    }

    public function clearAll(Request $request): Response
    {
        $this->model->clearAll(Session::get('user_id'));
        return Response::json(['ok' => true]);
    }
}
