<?php

namespace App\State\Processor;

use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\Validator\ValidatorInterface;
use App\Controller\OgBoot\OgLive\InstallAction;
use App\Controller\OgBoot\OgLive\UninstallAction;
use App\Dto\Input\OgLiveInput;
use App\Dto\Output\OgLiveOutput;
use App\Model\OgLiveStatus;
use App\Repository\OgLiveRepository;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

readonly class OgLiveProcessor implements ProcessorInterface
{
    public function __construct(
        private OgLiveRepository            $ogLiveRepository,
        private ValidatorInterface          $validator,
        private InstallAction               $installAction,
        private UninstallAction             $uninstallAction,
        private KernelInterface             $kernel,
    )
    {
    }

    /**
     * @throws \Exception
     */
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): OgLiveOutput|null
    {
        switch ($operation){
            case $operation instanceof Post:
            case $operation instanceof Put:
            case $operation instanceof Patch:
                return $this->processCreateOrUpdate($data, $operation, $uriVariables, $context);
            case $operation instanceof Delete:
                return $this->processDelete($data, $operation, $uriVariables, $context);
        }
    }

    /**
     * @throws \Exception
     * @throws TransportExceptionInterface
     */
    private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): OgLiveOutput
    {
        if (!($data instanceof OgLiveInput)) {
            throw new \Exception(sprintf('data is not instance of %s', OgLiveInput::class));
        }

        $entity = null;
        if (isset($uriVariables['uuid'])) {
            $entity = $this->ogLiveRepository->findOneByUuid($uriVariables['uuid']);
        }

        $ogLive = $data->createOrUpdateEntity($entity);
        $this->validator->validate($ogLive);
        $this->ogLiveRepository->save($ogLive);

        if ($this->kernel->getEnvironment() !== 'test') {
            if ($operation instanceof Post) {
                $this->installAction->__invoke($ogLive);
            }
        }

        return new OgLiveOutput($ogLive);
    }

    /**
     * @throws TransportExceptionInterface
     * @throws ServerExceptionInterface
     * @throws RedirectionExceptionInterface
     * @throws ClientExceptionInterface
     */
    private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null
    {
        $ogLive = $this->ogLiveRepository->findOneByUuid($uriVariables['uuid']);

        if ($this->kernel->getEnvironment() !== 'test') {
            if ($ogLive->getStatus() === OgLiveStatus::ACTIVE) {
                $this->uninstallAction->__invoke($ogLive);
            }
        }
        $this->ogLiveRepository->delete($ogLive);

        return null;
    }
}
