<?php

namespace App\Http\Controllers;

use Illuminate\Database\Eloquent\QUERY;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class NotificationsController extends Controller
{
    public function index()
    {
        try
        {
            $user = Auth::user();
            $userNotifications = $user->notifications;

            return response()->json(['notifications' => $userNotifications]);
        }
        catch (Exception $e)
        {
            return response()->json(null);
        }
    }

    public function getLastNotifications()
    {
        try
        {
            $user = Auth::user();
            $userUnreadNotifications = $user->unreadNotifications;

            return response()->json(['notifications' => $userUnreadNotifications]);
        }
        catch (Exception $e)
        {
            return response()->json(null);
        }
    }

    public function store(Request $request)
    {
        try
        {
            $user = Auth::user();
            $userUnreadNotifications = $user->unreadNotifications->markAsRead();

            return response()->json(['notifications' => $userUnreadNotifications]);
        }
        catch (Exception $e)
        {
            return response()->json(null);
        }
    }
}
