<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CoreBundle\Twig;

use Ferienpass\CoreBundle\Entity\Tenant;
use Ferienpass\CoreBundle\Tenant\TenantContext;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
 * Read-only view of the tenant context for templates: whether this build is
 * multi-tenant, and which municipality the current request works in. Templates
 * only ever display this — activating a tenant stays with {@see TenantContext}.
 */
class TenantExtension extends AbstractExtension
{
    public function __construct(private readonly TenantContext $tenantContext)
    {
    }

    public function getFunctions(): array
    {
        return [
            new TwigFunction('multi_tenancy_enabled', [$this, 'isEnabled']),
            new TwigFunction('current_tenant', [$this, 'getCurrentTenant']),
        ];
    }

    public function isEnabled(): bool
    {
        return $this->tenantContext->isEnabled();
    }

    public function getCurrentTenant(): ?Tenant
    {
        return $this->tenantContext->getTenant();
    }
}
