<?php

declare(strict_types=1);

namespace App\Shared\Infrastructure\Tenant;

final class TenantContext
{
    private ?TenantConfig $currentTenant = null;

    public function setCurrentTenant(TenantConfig $config): void
    {
        $this->currentTenant = $config;
    }

    public function getCurrentTenantId(): TenantId
    {
        if ($this->currentTenant === null) {
            throw new TenantNotSetException('No tenant is set in the current context.');
        }

        return $this->currentTenant->tenantId;
    }

    public function getCurrentTenantConfig(): TenantConfig
    {
        if ($this->currentTenant === null) {
            throw new TenantNotSetException('No tenant is set in the current context.');
        }

        return $this->currentTenant;
    }

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

    public function clear(): void
    {
        $this->currentTenant = null;
    }
}
