<?php
namespace App\Services\Auth\User;
use App\Models\Visitor;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

class oldAccountService
{
    public function getAccount()
    {
        if(Auth::user())
        {
            $user = Auth::user();
            $status = 'user';
        }
        else
        {
            // $token = Session::get('token');
            if(isset($_COOKIE['token'])) 
            {
                $token = $_COOKIE['token'];
            }
            else
            {
                $token = null;
            }
            $user = Visitor::where('token',$token)->first();
            
            $status = 'visitor';
        }
        return [
            'status'=>$status,
            'user'=>$user
        ];
    }
    public function setAccount()
    {
        $user = $this->getAccount();

        if(!$user['user'])
        {
            $token = bcrypt(rand(11111,99999).time());
            // Session::put('token', $token);
            setcookie('token', $token, time() + (86400 * 30 * 30), "/"); 
            $user = Visitor::create([
                'token' => $token,
                'currency_id' => 1
            ]);

            $user = [
                'status'=>'visitor',
                'user'=>$user
            ];
        }
        return $user;
    }
}
