<?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\Tests\Entity;

use Ferienpass\CoreBundle\Entity\Tenant;
use Ferienpass\CoreBundle\Entity\TenantDomain;
use Ferienpass\CoreBundle\Tenant\Surface;
use PHPUnit\Framework\TestCase;

class TenantTest extends TestCase
{
    public function testADomainServesThePublicSiteUnlessItSaysOtherwise(): void
    {
        $tenant = new Tenant();
        $tenant->addDomain(new TenantDomain($tenant, 'ferienpass-nordhorn.de'));

        self::assertTrue($tenant->hasFrontend());
        self::assertFalse($tenant->hasDedicatedAdminHost());
        self::assertSame(['ferienpass-nordhorn.de'], $tenant->getFrontendHosts());
        self::assertSame([], $tenant->getAdminHosts());
    }

    /**
     * Links generated without a request go to parents more often than to
     * staff, so the public site is the tenant's primary host.
     */
    public function testThePublicSiteIsThePrimaryHost(): void
    {
        $tenant = new Tenant();
        $tenant->addDomain(new TenantDomain($tenant, 'nordhorn.my-ferienpass.de', Surface::Admin));
        $tenant->addDomain(new TenantDomain($tenant, 'ferienpass-nordhorn.de', Surface::Frontend));
        $tenant->addDomain(new TenantDomain($tenant, 'www.ferienpass-nordhorn.de', Surface::Frontend));

        self::assertSame('ferienpass-nordhorn.de', $tenant->getPrimaryHost());
        self::assertSame('ferienpass-nordhorn.de', $tenant->getPrimaryHostFor(Surface::Frontend));
        self::assertSame('nordhorn.my-ferienpass.de', $tenant->getPrimaryHostFor(Surface::Admin));
    }

    public function testATenantWithoutAPublicSiteFallsBackToItsAdminDomain(): void
    {
        $tenant = new Tenant();
        $tenant->addDomain(new TenantDomain($tenant, 'alfeld.my-ferienpass.de', Surface::Admin));

        self::assertFalse($tenant->hasFrontend());
        self::assertTrue($tenant->hasDedicatedAdminHost());
        self::assertSame('alfeld.my-ferienpass.de', $tenant->getPrimaryHost());
        self::assertNull($tenant->getPrimaryHostFor(Surface::Frontend));
    }
}
