<?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\PageConfigManager;
use Ferienpass\CoreBundle\Settings\SystemSettingsManager;
use PHPUnit\Framework\TestCase;

class SystemSettingsManagerTest extends TestCase
{
    private $pageConfigManager;

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

    public function testFriendsBookingPrecedence(): void
    {
        $pageConfig = new PageConfig();
        $pageConfig->setFriendsBooking(false);
        $this->pageConfigManager->method('getPageConfig')->willReturn($pageConfig);

        $manager = new SystemSettingsManager(
            $this->pageConfigManager,
            true // config value
        );

        $this->assertTrue($manager->isFriendsBookingFromConfig());
        $this->assertTrue($manager->isFriendsBooking());
    }

    public function testPreferredPostalCodesPrecedence(): void
    {
        $pageConfig = new PageConfig();
        $pageConfig->setPreferredPostalCodes('12345,67890');
        $this->pageConfigManager->method('getPageConfig')->willReturn($pageConfig);

        $manager = new SystemSettingsManager(
            $this->pageConfigManager,
            null,
            ['11111', '22222'] // config value
        );

        $this->assertTrue($manager->isPreferredPostalCodesFromConfig());
        $this->assertSame('11111,22222', $manager->getPreferredPostalCodes());
        $this->assertSame(['11111', '22222'], $manager->getPreferredPostalCodesArray());
    }

    public function testEraseDataPeriodPrecedence(): void
    {
        $pageConfig = new PageConfig();
        $pageConfig->setEraseDataPeriod('P1Y');
        $this->pageConfigManager->method('getPageConfig')->willReturn($pageConfig);

        $manager = new SystemSettingsManager(
            $this->pageConfigManager,
            null,
            null,
            null,
            'P2Y' // erase data period config
        );

        $this->assertTrue($manager->isEraseDataPeriodFromConfig());
        $this->assertSame('P2Y', $manager->getEraseDataPeriod());
    }

    public function testAgeTolerancePrecedence(): void
    {
        $pageConfig = new PageConfig();
        $pageConfig->setAgeTolerance('none');
        $this->pageConfigManager->method('getPageConfig')->willReturn($pageConfig);

        $manager = new SystemSettingsManager(
            $this->pageConfigManager,
            null,
            null,
            null,
            null,
            null,
            null,
            'full_year' // age tolerance config
        );

        $this->assertTrue($manager->isAgeToleranceFromConfig());
        $this->assertSame('full_year', $manager->getAgeTolerance());
    }

    public function testRemindAttendancePrecedence(): void
    {
        $pageConfig = new PageConfig();
        $pageConfig->setRemindAttendance(false);
        $this->pageConfigManager->method('getPageConfig')->willReturn($pageConfig);

        $manager = new SystemSettingsManager(
            $this->pageConfigManager,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            true // remind attendance config
        );

        $this->assertTrue($manager->isRemindAttendanceFromConfig());
        $this->assertTrue($manager->isRemindAttendance());
    }

    public function testFallbackToDb(): void
    {
        $pageConfig = new PageConfig();
        $pageConfig->setFriendsBooking(true);
        $pageConfig->setPreferredPostalCodes('12345');
        $this->pageConfigManager->method('getPageConfig')->willReturn($pageConfig);

        $manager = new SystemSettingsManager(
            $this->pageConfigManager
        );

        $this->assertFalse($manager->isFriendsBookingFromConfig());
        $this->assertTrue($manager->isFriendsBooking());
        $this->assertSame('12345', $manager->getPreferredPostalCodes());
    }
}
