<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Notifications\CustomNotification;
use App\Http\Resources\UserLoggedResource;

class NotificationController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:sanctum');
    }

    public function markAsRead(Request $request)
    {
        $notification = auth()->user()->notifications()->find($request->id);
        if ($notification) {
            $notification->markAsRead();
            return response()->json(new UserLoggedResource(auth()->user()));
        }
    }

    public function markAsUnread(Request $request)
    {
        $notification = auth()->user()->notifications()->find($request->id);
        if ($notification) {
            $notification->read_at = null;
            $notification->save();
            return response()->json(new UserLoggedResource(auth()->user()));
        }
    }

    public function delete(Request $request)
    {
        $notification = auth()->user()->notifications()->find($request->id);
        if ($notification) {
            $notification->delete();
            return response()->json(new UserLoggedResource(auth()->user()));
        }
    }

    public function test()
    {
        if (!auth()->user()) return;

        $demo_notification = new CustomNotification(
            'Subject',
            'Lorem ipsum dolor sit amet consectetur adipisicing elit. Ab dolores libero at dolorem unde, consequuntur sed eveniet totam aperiam aspernatur.'
        );

        auth()->user()->notify($demo_notification);
        broadcast(new \App\Events\UserInfoUpdated(auth()->user()));
    }
}
