<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Tests\AgeVerification;

use CpCreation\VitiCore\AgeVerification\Model\AgeVerificationConfig;
use CpCreation\VitiCore\AgeVerification\Model\AgeVerificationCountryRule;
use PHPUnit\Framework\TestCase;

final class AgeVerificationConfigTest extends TestCase
{
    public function testItIsDisabledByDefaultWithSafeFallbacks(): void
    {
        $config = new AgeVerificationConfig();

        self::assertFalse($config->isEnabled());
        self::assertSame('FR', $config->getDefaultCountryCode());
        self::assertSame(21, $config->getDefaultMinimumAge());
        self::assertSame(30, $config->getRememberForDays());
        self::assertCount(0, $config->getCountryRules());
    }

    public function testItResolvesConfiguredCountryAgeAndDefaultAge(): void
    {
        $config = new AgeVerificationConfig();
        $config->addCountryRule(new AgeVerificationCountryRule('US', 21));

        self::assertSame(18, $config->resolveMinimumAge('fr'));
        self::assertSame(21, $config->resolveMinimumAge('US'));
        self::assertSame(21, $config->resolveMinimumAge('AR'));
    }

    public function testItUsesBuiltInConservativeAgesBeforeTheDefaultFallback(): void
    {
        $config = new AgeVerificationConfig();

        foreach (['FR', 'BE', 'GB', 'DE', 'IT', 'NL', 'ES'] as $countryCode) {
            self::assertSame(18, $config->resolveMinimumAge($countryCode));
        }
        self::assertSame(21, $config->resolveMinimumAge('US'));
        self::assertSame(21, $config->resolveMinimumAge('ZZ'));
    }

    public function testAdministrationRuleOverridesBuiltInCountryAge(): void
    {
        $config = new AgeVerificationConfig();
        $config->addCountryRule(new AgeVerificationCountryRule('BE', 21));

        self::assertSame(21, $config->resolveMinimumAge('BE'));
    }

    public function testRemovingACountryRuleClearsItsParent(): void
    {
        $config = new AgeVerificationConfig();
        $rule = new AgeVerificationCountryRule('BE', 18);
        $config->addCountryRule($rule);

        $config->removeCountryRule($rule);

        self::assertNull($rule->getConfig());
        self::assertFalse($config->getCountryRules()->contains($rule));
    }

    public function testProofFingerprintChangesWithCountryRules(): void
    {
        $config = new AgeVerificationConfig();
        $fingerprint = $config->getProofFingerprint();
        $config->addCountryRule(new AgeVerificationCountryRule('GY', 20));

        self::assertNotSame($fingerprint, $config->getProofFingerprint());
    }
}
