<?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\PxeBootFile\DeleteAction;
use App\Controller\OgDhcp\Subnet\DeleteHostAction;
use App\Dto\Input\ClientInput;
use App\Dto\Output\ClientOutput;
use App\Repository\ClientRepository;
use App\Repository\MenuRepository;
use App\Repository\OgLiveRepository;
use App\Repository\PxeTemplateRepository;
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 ClientProcessor implements ProcessorInterface
{
    public function __construct(
        private ClientRepository        $clientRepository,
        private MenuRepository          $menuRepository,
        private PxeTemplateRepository   $pxeTemplateRepository,
        private OgLiveRepository        $ogLiveRepository,
        private ValidatorInterface      $validator,
        private DeleteHostAction        $deleteHostAction,
        private DeleteAction            $deletePxeAction,
        private KernelInterface         $kernel,
    )
    {
    }

    /**
     * @throws \Exception
     */
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
    {
        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
     */
    private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): ClientOutput
    {
        if (!($data instanceof ClientInput)) {
            throw new \Exception(sprintf('data is not instance of %s', ClientInput::class));
        }

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

        $defaultMenu = $this->menuRepository->findOneBy(['isDefault' => true]);
        $defaultPxe = $this->pxeTemplateRepository->findOneBy(['isDefault' => true]);
        $defaultPxeOgLive = $this->ogLiveRepository->findOneBy(['isDefault' => true]);

        $client = $data->createOrUpdateEntity($entity);

        if ($defaultMenu && !$client->getMenu()) {
            $client->setMenu($defaultMenu);
            $client->setResolution($defaultMenu->getResolution());
        }

        if ($defaultPxe && !$client->getTemplate()) {
            $client->setTemplate($defaultPxe);
        }

        if ($defaultPxeOgLive && !$client->getOgLive()) {
            $client->setOgLive($defaultPxeOgLive);
        }

        $this->validator->validate($client);
        $this->clientRepository->save($client);

        return new ClientOutput($client);
    }

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

        if ($this->kernel->getEnvironment() !== 'test') {
            if ($client->getSubnet()) {
                $this->deleteHostAction->__invoke($client->getSubnet(), $client->getUuid());
            }
            $this->deletePxeAction->__invoke($client->getMac());
        }

        $this->clientRepository->delete($client);

        return null;
    }
}
