<?php

declare(strict_types=1);

namespace App\Http\Controllers\Notification;

use App\Http\Controllers\Controller;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ReadNotificationController extends Controller
{
    /**
     * Handle the incoming request.
     */
    public function __invoke(Request $request)
    {
        $user = Auth::guard('api')->user();
        if (! $user) {
            return ApiResponse::response(message: 'اطلاعات وارد شده صحیح نیست', status: 422);
        }

        if (property_exists($request, 'notification') && $request->notification !== null) {
            $user->unreadNotifications()->where('id', $request->notification)->first()?->markAsRead();

            return ApiResponse::success(message: 'با موفقیت انجام شد.');
        } else {
            $user->unreadNotifications()->update(['read_at' => now()]);

            return ApiResponse::success(message: 'با موفقیت انجام شد.');
        }
    }
}
