<?php
namespace Modules\User\Services;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Modules\User\Enums\AccountStatusEnum;
use Modules\User\Models\Visitor;

class AccountService
{
    public function getAccount()
    {
        if(Auth::user())
        {
            $user = Auth::user();
            $status = AccountStatusEnum::USER->value;
        }
        else
        {
            $user = $this->setAccount();
            
            $token = $this->setAccount();

            $status = AccountStatusEnum::VISITOR->value;
        }
        return [
            'status'=>$status,
            'user'=>$user
        ];
    }
    public function setAccount()
    {
        $token = request()->header('tokenDevice');
        $visitor = Visitor::where('token',$token)->first();
        if ($token && !$visitor)
        {
//            $token = bcrypt(rand(11111, 99999) . time());
//            Session::put('token', $token);
            $visitor = Visitor::create([
                'token' => $token,
            ]);
        }
        return $visitor;
    }
    public function update(Request $request)
    {
        $account = $this->getAccount();
        $account['user']->update($request->all());
    }
}
