<?php

declare(strict_types=1);

namespace Ferienpass\CoreBundle\Tests\Settings;

use Ferienpass\CoreBundle\Entity\PageConfig;
use Ferienpass\CoreBundle\Settings\MailConfigManager;
use Ferienpass\CoreBundle\Settings\PageConfigManager;
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,
            '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,
            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,
            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,
            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->assertSame('Admin Name', $manager->getAdminEmailName());
    }
}
