<?php

namespace NoahVet\Reef\Infrastructure\Serialization\Factory;

use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
use Symfony\Component\Serializer\Serializer;

class JsonSerializerFactory implements JsonSerializerFactoryInterface
{
    #[\Override]
    public function createSerializer(
        array $normalizers,
        ?ClassMetadataFactoryInterface $classMetadataFactory = null,
        ?string $timezone = 'UTC',
    ): Serializer {
        return new Serializer(
            \array_merge(
                $normalizers,
                [
                    new DateTimeNormalizer([DateTimeNormalizer::TIMEZONE_KEY => $timezone]),
                    new BackedEnumNormalizer(),
                    new UidNormalizer(),
                    new ArrayDenormalizer(),
                    new ObjectNormalizer($classMetadataFactory),
                    new GetSetMethodNormalizer(),
                ],
            ),
            [
                new JsonEncoder(defaultContext: [
                    'json_encode_options' => \JSON_PRESERVE_ZERO_FRACTION,
                ]),
            ],
        );
    }
}
