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

use Ferienpass\CoreBundle\Entity\PageConfig;
use Ferienpass\CoreBundle\Settings\MailConfigManager;
use Ferienpass\CoreBundle\Settings\PageConfigManager;
use Ferienpass\CoreBundle\Tenant\TenantContext;
use PHPUnit\Framework\TestCase;

class MailConfigManagerTest extends TestCase
{
    private $pageConfigManager;

    protected function setUp(): void
    {
        $this->pageConfigManager = $this->createStub(PageConfigManager::class);
    }

    public function testGetAdminEmailFromEnvPrecedence(): void
    {
        $pageConfig = new PageConfig();
        $pageConfig->setAdminEmail('db@example.com');

        $this->pageConfigManager->method('getPageConfig')->willReturn($pageConfig);

        $manager = new MailConfigManager(
            $this->pageConfigManager,
            $this->tenantContext(false),
            'env@example.com',
            null
        );

        $this->assertTrue($manager->isAdminEmailFromEnv());
        $this->assertSame('env@example.com', $manager->getAdminEmail());
    }

    public function testGetAdminEmailFromDbFallback(): void
    {
        $pageConfig = new PageConfig();
        $pageConfig->setAdminEmail('db@example.com');

        $this->pageConfigManager->method('getPageConfig')->willReturn($pageConfig);

        $manager = new MailConfigManager(
            $this->pageConfigManager,
            $this->tenantContext(false),
            null,
            null
        );

        $this->assertFalse($manager->isAdminEmailFromEnv());
        $this->assertSame('db@example.com', $manager->getAdminEmail());
    }

    public function testGetReplyToFromConfigPrecedence(): void
    {
        $pageConfig = new PageConfig();
        $pageConfig->setReplyToEmail('db@example.com');

        $this->pageConfigManager->method('getPageConfig')->willReturn($pageConfig);

        $manager = new MailConfigManager(
            $this->pageConfigManager,
            $this->tenantContext(false),
            null,
            'config@example.com'
        );

        $this->assertSame('config@example.com', $manager->getReplyTo());
    }

    public function testGetReplyToFromDbFallback(): void
    {
        $pageConfig = new PageConfig();
        $pageConfig->setReplyToEmail('db@example.com');

        $this->pageConfigManager->method('getPageConfig')->willReturn($pageConfig);

        $manager = new MailConfigManager(
            $this->pageConfigManager,
            $this->tenantContext(false),
            null,
            null
        );

        $this->assertSame('db@example.com', $manager->getReplyTo());
    }

    public function testGetAdminEmailName(): void
    {
        $pageConfig = new PageConfig();
        $pageConfig->setAdminEmailName('Admin Name');

        $this->pageConfigManager->method('getPageConfig')->willReturn($pageConfig);

        $manager = new MailConfigManager(
            $this->pageConfigManager,
            $this->tenantContext(false)
        );

        $this->assertSame('Admin Name', $manager->getAdminEmailName());
    }

    /**
     * A build-wide sender is fine for one municipality, so ADMIN_EMAIL wins —
     * unless several tenants share the installation, where it must not.
     */
    public function testTheTenantsOwnAdminEmailWinsOverTheEnvironment(): void
    {
        $pageConfig = new PageConfig();
        $pageConfig->setAdminEmail('nordhorn@example.com');

        $this->pageConfigManager->method('getPageConfig')->willReturn($pageConfig);

        $manager = new MailConfigManager(
            $this->pageConfigManager,
            $this->tenantContext(true),
            'shared@example.com',
            null
        );

        $this->assertFalse($manager->isAdminEmailFromEnv());
        $this->assertSame('nordhorn@example.com', $manager->getAdminEmail());
    }

    private function tenantContext(bool $enabled): TenantContext
    {
        $tenantContext = $this->createStub(TenantContext::class);
        $tenantContext->method('isEnabled')->willReturn($enabled);

        return $tenantContext;
    }
}
