<?php

declare(strict_types=1);

/*
 * 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\Controller\Admin;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Controller\Admin\CatalogConfigurationController;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Http\Attribute\IsGranted;

final class CatalogConfigurationControllerTest extends TestCase
{
    public function testTheWholeAdministrationPageRequiresTheSuperAdministratorRole(): void
    {
        $attributes = new \ReflectionClass(CatalogConfigurationController::class)
            ->getAttributes(IsGranted::class);

        self::assertCount(1, $attributes);
        self::assertSame('ROLE_SUPER_ADMIN', $attributes[0]->newInstance()->attribute);
    }

    public function testTheTemplateNeverReferencesStoredSecretsOrRawErrors(): void
    {
        $template = file_get_contents(
            \dirname(__DIR__, 3) . '/src/Bridge/Symfony/Bundle/Resources/views/admin/commerce/configuration.html.twig',
        );

        self::assertIsString($template);
        foreach ([
            'enrollmentTokenCiphertext',
            'revealEnrollmentToken',
            'webhookSecret',
            'authorizationStateHash',
            'authorizationPkceVerifier',
            'jwtPublicKeyPem',
            'lastError',
        ] as $sensitiveProperty) {
            self::assertStringNotContainsString($sensitiveProperty, $template);
        }
    }

    public function testTheMenuEntryIsAlsoRestrictedToSuperAdministrators(): void
    {
        $template = file_get_contents(
            \dirname(__DIR__, 3) . '/src/Bridge/Symfony/Bundle/Resources/views/admin/base.html.twig',
        );

        self::assertIsString($template);
        self::assertMatchesRegularExpression(
            "/\\{% if is_granted\\('ROLE_SUPER_ADMIN'\\) %}.*admin_commerce_configuration.*\\{% endif %}/s",
            $template,
        );
    }

    public function testTheAdministrationUsesTheSerenityBrand(): void
    {
        $template = file_get_contents(
            \dirname(__DIR__, 3) . '/src/Bridge/Symfony/Bundle/Resources/views/admin/base.html.twig',
        );
        $translations = file_get_contents(
            \dirname(__DIR__, 3) . '/src/Bridge/Symfony/Bundle/Resources/translations/messages.fr.yaml',
        );

        self::assertIsString($template);
        self::assertIsString($translations);
        self::assertStringContainsString("{{ 'admin.brand'|trans }}", $template);
        self::assertStringContainsString('brand: Admin - Serenity', $translations);
        self::assertStringNotContainsString('Admin - VitiCore', $template);
    }

    public function testTheCommerceConfigurationIsExcludedFromAuditLogs(): void
    {
        $auditorConfiguration = file_get_contents(
            \dirname(__DIR__, 3) . '/src/Bridge/Symfony/Bundle/Resources/config/packages/dh_auditor.yaml',
        );

        self::assertIsString($auditorConfiguration);
        self::assertMatchesRegularExpression(
            '/CatalogConfiguration:\\s+enabled: false/',
            $auditorConfiguration,
        );
    }
}
