<?php
require_once __DIR__ . '/../models/Notification.php';
require_once __DIR__ . '/../helpers/Response.php';
require_once __DIR__ . '/../middlewares/AuthMiddleware.php';

class NotificationController
{
    private $db;
    private $notification;

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

    public function list()
    {
        $user = AuthMiddleware::authenticate();
        $notifications = $this->notification->getUserNotifications($user->id);

        // Also return unread count
        $unread = $this->notification->getUnreadCount($user->id);

        Response::success("Notificações recuperadas.", [
            'list' => $notifications,
            'unread_count' => $unread
        ]);
    }

    public function markAsRead()
    {
        $user = AuthMiddleware::authenticate();
        $data = json_decode(file_get_contents("php://input"), true);
        $id = $data['id'] ?? null;

        if (!$id) {
            Response::error("ID da notificação obrigatório.", 400);
        }

        if ($this->notification->markAsRead($id, $user->id)) {
            Response::success("Notificação marcada como lida.");
        } else {
            Response::error("Erro ao atualizar notificação.", 500);
        }
    }
}
?>