<?php

declare(strict_types=1);

namespace Ferienpass\AdminBundle\Tests\Export;

use Ferienpass\AdminBundle\Export\SerializerContextBuilder;
use Ferienpass\CoreBundle\Entity\PageConfig;
use Ferienpass\CoreBundle\Settings\PageConfigManager;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;

final class SerializerContextBuilderTest extends TestCase
{
    private function createBuilder(Security $security): SerializerContextBuilder
    {
        $pageConfig = new PageConfig();
        $pageConfig->setTimezone('Europe/Berlin');

        $pageConfigManager = $this->createMock(PageConfigManager::class);
        $pageConfigManager->method('getPageConfig')->willReturn($pageConfig);

        return new SerializerContextBuilder($security, $pageConfigManager);
    }

    public function testBuildReturnsAdminGroupWhenUserIsAdmin(): void
    {
        $security = $this->createMock(Security::class);
        $security->method('isGranted')->with('ROLE_ADMIN')->willReturn(true);

        $builder = $this->createBuilder($security);
        $context = $builder->build();

        self::assertSame('list:export:admin', $context['groups']);
    }

    public function testBuildReturnsDefaultGroupWhenUserIsNotAdmin(): void
    {
        $security = $this->createMock(Security::class);
        $security->method('isGranted')->with('ROLE_ADMIN')->willReturn(false);

        $builder = $this->createBuilder($security);
        $context = $builder->build();

        self::assertSame('list:export:default', $context['groups']);
    }

    public function testBuildIncludesDateTimeFormat(): void
    {
        $security = $this->createMock(Security::class);
        $security->method('isGranted')->willReturn(false);

        $builder = $this->createBuilder($security);
        $context = $builder->build();

        self::assertSame('d.m.Y H:i:s', $context[DateTimeNormalizer::FORMAT_KEY]);
    }

    public function testBuildIncludesTimezone(): void
    {
        $security = $this->createMock(Security::class);
        $security->method('isGranted')->willReturn(false);

        $builder = $this->createBuilder($security);
        $context = $builder->build();

        self::assertSame('Europe/Berlin', $context[DateTimeNormalizer::TIMEZONE_KEY]);
    }
}
