<?php

declare(strict_types=1);

namespace App\State\Processor\Shop;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Post;
use ApiPlatform\State\ProcessorInterface;
use App\Enum\Shop\ShopRequestType;
use App\Service\Shop\ShopService;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\SerializerInterface;

/**
 * Processor for handling new structure trial requests for the artist premium product.
 */
class NewStructureArtistPremiumTrialRequestProcessor implements ProcessorInterface
{
    public function __construct(
        private readonly ShopService $shopService,
        private readonly SerializerInterface $serializer,
    ) {
    }

    /**
     * @param mixed $data
     * @param Operation $operation
     * @param array $uriVariables
     * @param array $context
     * @return mixed
     */
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
    {
        if (!$operation instanceof Post) {
            throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
        }

        // Serialize the entity to JSON and decode to array
        $jsonData = $this->serializer->serialize($data, 'json');
        $requestData = json_decode($jsonData, true);

        // Create the shop request
        $this->shopService->registerNewShopRequest(
            ShopRequestType::NEW_STRUCTURE_ARTIST_PREMIUM_TRIAL,
            $requestData
        );

        return $data;
    }
}
