<?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\AgeVerificationProofManager;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

final class AgeVerificationProofManagerTest extends TestCase
{
    public function testItAcceptsAnUnexpiredSignedProof(): void
    {
        $manager = new AgeVerificationProofManager('test-secret');
        $now = new \DateTimeImmutable('2026-07-22 12:00:00');
        $request = Request::create('https://example.test/');
        $cookie = $manager->createCookie($request, 30, 'configuration-a', $now);
        $request->cookies->set(AgeVerificationProofManager::COOKIE_NAME, $cookie->getValue());

        self::assertTrue($cookie->isSecure());
        self::assertTrue($cookie->isHttpOnly());
        self::assertTrue($manager->isVerified($request, 'configuration-a', $now->modify('+29 days')));
    }

    public function testItRejectsExpiredOrTamperedProofs(): void
    {
        $manager = new AgeVerificationProofManager('test-secret');
        $now = new \DateTimeImmutable('2026-07-22 12:00:00');
        $request = Request::create('https://example.test/');
        $cookie = $manager->createCookie($request, 1, 'configuration-a', $now);
        $request->cookies->set(AgeVerificationProofManager::COOKIE_NAME, $cookie->getValue());

        self::assertFalse($manager->isVerified($request, 'configuration-a', $now->modify('+2 days')));

        $request->cookies->set(AgeVerificationProofManager::COOKIE_NAME, $cookie->getValue() . 'tampered');
        self::assertFalse($manager->isVerified($request, 'configuration-a', $now));
    }

    public function testItRejectsAProofCreatedForAnOlderAgeConfiguration(): void
    {
        $manager = new AgeVerificationProofManager('test-secret');
        $now = new \DateTimeImmutable('2026-07-22 12:00:00');
        $request = Request::create('https://example.test/');
        $cookie = $manager->createCookie($request, 30, 'configuration-a', $now);
        $request->cookies->set(AgeVerificationProofManager::COOKIE_NAME, $cookie->getValue());

        self::assertTrue($manager->isVerified($request, 'configuration-a', $now));
        self::assertFalse($manager->isVerified($request, 'configuration-b', $now));
    }
}
