<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use App\Models\User;
use Modules\User\Services\AccountService;
use Modules\User\Services\UserService;

class AuthApi
{
    public function __construct(
        private UserService $userService,
        private AccountService $accountService
    ){}

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $token= $request->bearerToken();
        if($token)
        {
            $user = $this->userService->getItemByToken($token);
            if($user)
            {
                Auth::login($user);
            }
        }
        $this->accountService->getAccount();

        return $next($request);
    }
}
