<?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\Tenant;

use Ferienpass\CoreBundle\Entity\Offer;
use Ferienpass\CoreBundle\Entity\Tenant;
use Ferienpass\CoreBundle\Entity\User;
use Ferienpass\CoreBundle\Fixtures\Doctrine\OrmTestCase;
use Ferienpass\CoreBundle\Tenant\TenantContext;
use Ferienpass\CoreBundle\Tenant\TenantTables;

class TenantTablesTest extends OrmTestCase
{
    /**
     * Turning a single-tenant installation into the first tenant of a shared
     * one: everything that has no tenant becomes theirs, and stays visible.
     */
    public function testAdoptsOrphanedRows(): void
    {
        $tenant = $this->createTenant('nordhorn');

        $this->createOffer();
        $this->createOffer();
        $this->createUser('parent@example.com');
        $this->em->flush();
        $this->em->clear();

        $adopted = new TenantTables($this->em)->adoptOrphans($this->reload($tenant));

        $this->assertSame(2, $adopted['Offer']);
        $this->assertSame(1, $adopted['User']);

        $context = new TenantContext($this->registry, enabled: true);
        $context->setTenant($this->reload($tenant));

        $this->assertCount(2, $this->em->getRepository(Offer::class)->findAll());
        $this->assertCount(1, $this->em->getRepository(User::class)->findAll());
    }

    public function testLeavesRowsOfAnotherTenantAlone(): void
    {
        $mine = $this->createTenant('nordhorn');
        $theirs = $this->createTenant('luebeck');

        $this->createOffer()->setTenant($theirs);
        $this->createOffer();
        $this->em->flush();
        $this->em->clear();

        new TenantTables($this->em)->adoptOrphans($this->reload($mine));

        $context = new TenantContext($this->registry, enabled: true);
        $context->setTenant($this->reload($theirs));

        $this->assertCount(1, $this->em->getRepository(Offer::class)->findAll());
    }

    public function testCountsOnlyTablesThatHoldRows(): void
    {
        $tenant = $this->createTenant('nordhorn');

        $this->createOffer()->setTenant($tenant);
        $this->em->flush();
        $this->em->clear();

        $counts = new TenantTables($this->em)->countRows($this->reload($tenant));

        $this->assertSame(['Offer' => 1], $counts);
    }

    /**
     * The map is derived from the mapping, so a newly tenant-aware entity is
     * covered without touching this class.
     */
    public function testTheMapCoversTheTenantAwareEntities(): void
    {
        $map = new TenantTables($this->em)->map();

        $this->assertArrayHasKey('Offer', $map);
        $this->assertArrayHasKey('User', $map);
        $this->assertArrayNotHasKey('Tenant', $map);
        $this->assertSame('tenant_id', $map['Offer']);
    }

    /**
     * The admin names a tenant's leftover rows after the entity behind the
     * table, so both views must stay aligned.
     */
    public function testTheTableClassesAlignWithTheMap(): void
    {
        $tables = new TenantTables($this->em);
        $classes = $tables->classByTable();

        $this->assertSame(Offer::class, $classes['Offer']);
        $this->assertSame(User::class, $classes['User']);
        $this->assertArrayNotHasKey('Tenant', $classes);
        $this->assertSame(array_keys($tables->map()), array_keys($classes));
    }

    private function createTenant(string $slug): Tenant
    {
        $tenant = new Tenant();
        $tenant->setSlug($slug);
        $tenant->setName(ucfirst($slug));

        $this->em->persist($tenant);
        $this->em->flush();

        return $tenant;
    }

    private function reload(Tenant $tenant): Tenant
    {
        return $this->em->getRepository(Tenant::class)->find($tenant->getId());
    }
}
