<?php

declare(strict_types=1);

namespace App\Controller;

use ApiPlatform\Validator\ValidatorInterface;
use App\Dto\Input\DeployImageInput;
use App\Entity\Command;
use App\Entity\Image;
use App\Model\ClientStatus;
use App\Entity\ImageImageRepository;
use App\Entity\OrganizationalUnit;
use App\Entity\Partition;
use App\Model\CommandTypes;
use App\Model\DeployMethodTypes;
use App\Model\TraceStatus;
use App\Service\Trace\CreateService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class DeployImageAction extends AbstractController
{
    public function __construct(
        protected readonly EntityManagerInterface $entityManager,
        protected readonly HttpClientInterface    $httpClient,
        protected readonly CreateService          $createService,
        protected readonly ValidatorInterface     $validator,
        public readonly \App\Controller\OgAgent\DeployImageAction $deployImageOgAgentAction,
        public readonly \App\Controller\OgRepository\Image\DeployImageAction $deployImageOgRepositoryAction,
    ) {
    }

    /**
     * @throws RedirectionExceptionInterface
     * @throws ClientExceptionInterface
     * @throws ServerExceptionInterface
     */
    public function __invoke(DeployImageInput $input, ImageImageRepository $image): JsonResponse
    {
        $this->validator->validate($input);

        $clientJobs = [];

        if ($input->type === 'monolithic') {
            $clientJobs = $this->handleMonolithicDeployment($input, $image);
        }

        return new JsonResponse(data: $clientJobs, status: Response::HTTP_OK);
    }

    private function handleMonolithicDeployment(DeployImageInput $input, ImageImageRepository $image): array
    {
        $clientJobs = [];

        switch ($input->method) {
            case DeployMethodTypes::UNICAST:
            case DeployMethodTypes::UNICAST_DIRECT:
                $clientJobs = $this->handleUnicastDeployment($input, $image);
                break;
            case DeployMethodTypes::MULTICAST_UFTP:
            case DeployMethodTypes::MULTICAST_UFTP_DIRECT:
            case DeployMethodTypes::MULTICAST_UDPCAST:
            case DeployMethodTypes::MULTICAST_UDPCAST_DIRECT:
                $clientJobs = $this->handleMulticastDeployment($input, $image);
                break;
            case DeployMethodTypes::TORRENT:
                $clientJobs = $this->handleTorrentDeployment($input, $image);
                break;
        }

        return $clientJobs;
    }

    private function handleUnicastDeployment(DeployImageInput $input, ImageImageRepository $image): array
    {
        $clientJobs = [];

        foreach ($input->clients as $client) {
            $inputData = $this->createInputData($input, $image, $client->getEntity());
            $jobId = $this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::UNICAST);
            
            if ($jobId) {
                $clientJobs[(string) '/clients/' . $client->getEntity()->getUuid()] = $jobId;
            }
        }

        return $clientJobs;
    }

    private function handleMulticastDeployment(DeployImageInput $input, ImageImageRepository $image): array
    {
        $clientJobs = [];

        foreach ($input->clients as $client) {
            $inputData = $this->createMulticastInputData($input, $image, $client->getEntity());
            
            try {
                $this->deployImageOgRepositoryAction->__invoke($input, $image, $client->getEntity());
            } catch (\Exception $e) {
                continue;
            }

            $jobId = $this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::MULTICAST);
            
            if ($jobId) {
                $clientJobs[(string) '/clients/' . $client->getEntity()->getUuid()] = $jobId;
            }
        }

        return $clientJobs;
    }

    private function handleTorrentDeployment(DeployImageInput $input, ImageImageRepository $image): array
    {
        $clientJobs = [];

        foreach ($input->clients as $client) {
            $inputData = $this->createTorrentInputData($input, $image, $client->getEntity());
            
            try {
                $response = $this->deployImageOgRepositoryAction->__invoke($input, $image, $client->getEntity(), $this->httpClient);
                
                if (is_array($response) && isset($response['code']) && $response['code'] === 500) {
                    throw new \Exception('Error del servidor OgRepository: ' . ($response['error'] ?? 'Error desconocido') . ' - ' . ($response['details'] ?? ''));
                }
                
            } catch (\Exception $e) {
                throw $e;
            }

            $jobId = $this->processDeployment($client->getEntity(), $input, $image, $inputData, DeployMethodTypes::TORRENT);
            
            if ($jobId) {
                $clientJobs[(string) '/clients/' . $client->getEntity()->getUuid()] = $jobId;
            }
        }

        return $clientJobs;
    }

    private function processDeployment($client, DeployImageInput $input, ImageImageRepository $image, array $inputData, string $deployType): ?string
    {
        $agentJobId = $this->deployImageOgAgentAction->__invoke($image, $input, $client, $deployType);
        
        if (!$agentJobId) {
            if ($input->queue) {
                $this->createService->__invoke($client, CommandTypes::DEPLOY_IMAGE, TraceStatus::PENDING, null, $inputData);
            }
            return null;
        }

        $this->createService->__invoke($client, CommandTypes::DEPLOY_IMAGE, TraceStatus::IN_PROGRESS, $agentJobId, $inputData);
        
        return $agentJobId;
    }

    private function createInputData(DeployImageInput $input, ImageImageRepository $image, $client): array
    {
        return [
            'method' => $input->method,
            'client' => $client->getUuid(),
            'image' => $image->getUuid(),
            'imageName' => $image->getName(),
            'numDisk' => (string) $input->diskNumber,
            'numPartition' => (string) $input->partitionNumber,
            'type' => $input->type,
        ];
    }

    private function createMulticastInputData(DeployImageInput $input, ImageImageRepository $image, $client): array
    {
        return array_merge($this->createInputData($input, $image, $client), [
            'mcastIp' => $input->mcastIp,
            'mcastPort' => $input->mcastPort,
            'mcastSpeed' => $input->mcastSpeed,
            'mcastMode' => $input->mcastMode,
        ]);
    }

    private function createTorrentInputData(DeployImageInput $input, ImageImageRepository $image, $client): array
    {
        return array_merge($this->createInputData($input, $image, $client), [
            'p2pMode' => $input->p2pMode,
            'p2pTime' => $input->p2pTime,
        ]);
    }
}
