<?php

namespace App\Tmdb\Framework\Serializer;

use App\Base\Enum\MediaType;
use App\Tmdb\Dto\TmdbEpisodeDto;
use App\Tmdb\TmdbResult;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class TmdbTvEpisodeResultDenormalizer implements DenormalizerInterface
{
    public function __construct(
        #[Autowire(service: 'serializer.normalizer.object')]
        private readonly NormalizerInterface $normalizer,
    ) {}

    public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): TmdbResult|TmdbEpisodeDto|array|null
    {
        /** @var TmdbEpisodeDto $result */
        $result = $this->normalizer->denormalize($data, TmdbEpisodeDto::class, $format, $context);
        return $result;
    }

    public function supportsDenormalization(
        mixed $data,
        string $type,
        ?string $format = null,
        array $context = []
    ): bool {
        return array_key_exists('media_type', $context) &&
            $context['media_type'] === MediaType::TvEpisode->value;
    }

    public function getSupportedTypes(?string $format): array
    {
        return [
            TmdbResult::class => false,
        ];
    }
}
