<?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
        {
            $token = Session::get('token');
            if (!$token)
            {
                $token = $this->setAccount();
            }
            $user = Visitor::where('token',$token)->first();
            if (!$user)
            {
                $token = $this->setAccount();
                $user = Visitor::where('token',$token)->first();
            }
            $status = AccountStatusEnum::VISITOR->value;
        }
        return [
            'status'=>$status,
            'user'=>$user
        ];
    }
    public function setAccount()
    {
        $token = Session::get('token');
        if (!$token)
        {
//            $token = bcrypt(rand(11111, 99999) . time());
//            Session::put('token', $token);
//            Visitor::create([
//                'token' => $token,
//            ]);
        }
        return $token;
    }
    public function update(Request $request)
    {
        $account = $this->getAccount();
        $account['user']->update($request->all());
    }
}
