<?php

namespace App\Proxy;

use App\Auth\XenForoUser;
use App\Services\XenforoService;

/**
 * @mixin XenForoUser
 */
class VisitorProxy
{
    private ?XenForoUser $currentVisitor;
    private array $users = [];

    public function __construct(?XenForoUser $user)
    {
        $this->currentVisitor = $user;
    }

    public function __get( string $name ): mixed
    {
        return $this->currentVisitor?->$name;
    }

    public function __invoke( int $userId ): ?XenForoUser
    {
        if( !isset( $this->users[$userId] ) ){
            $this->users[$userId] = app(XenforoService::class)->getXfUser($userId);
        }
        return $this->users[$userId];
    }

    public function loggedIn(): bool
    {
        return $this->currentVisitor !== null;
    }

    public function guest(): bool
    {
        return $this->currentVisitor === null;
    }
}
