<?php

namespace App\Services;

use Illuminate\Support\Facades\Auth;

class NotificationService
{
    public function getNotifications()
    {
        return auth()->user()->notifications()->latest()->get();
    }

    public function getMessageDetails($notification)
    {
        if (method_exists($notification->type, 'getMessageDetails')) {
            return app($notification->type)->getMessageDetails($notification->data);
        }

        return null;
    }

    public function markAllAsRead()
    {
        auth()->user()->unreadNotifications->markAsRead();
    }

    public function markAsRead($notificationId)
    {
        $notification = auth()->user()->notifications()->find($notificationId);
        if ($notification) {
            $notification->markAsRead();
            return true;
        }
        return false;
    }

    public function deleteAllNotifications()
    {
        auth()->user()->notifications()->delete();
    }
}
