<?php

declare(strict_types=1);

namespace App\Controller;

use ApiPlatform\Validator\ValidatorInterface;
use App\Dto\Input\UpdateGitImageInput;
use App\Entity\Client;
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;

class UpdateGitImageAction extends AbstractController
{
    public function __construct(
        protected readonly EntityManagerInterface $entityManager,
        protected readonly CreateService          $createService,
        protected readonly ValidatorInterface     $validator,
        public readonly \App\Controller\OgAgent\UpdateGitImageAction $updateGitImageOgAgentAction,
    ) {
    }

    /**
     * @throws RedirectionExceptionInterface
     * @throws ClientExceptionInterface
     * @throws ServerExceptionInterface
     */
    public function __invoke(UpdateGitImageInput $input): JsonResponse
    {
        $this->validator->validate($input);
        
        if (!$input->client) {
            throw new \InvalidArgumentException('Client is required');
        }

        if (!$input->partition) {
            throw new \InvalidArgumentException('Partition is required');
        }

        $this->handleGitUpdate($input);

        return new JsonResponse(data: [], status: Response::HTTP_OK);
    }

    private function handleGitUpdate(UpdateGitImageInput $input): void
    {
        $client = $input->client->getEntity();
        $partition = $input->partition->getEntity();
        
        $inputData = $this->createInputData($input, $client, $partition);
        $this->processUpdate($client, $input, $inputData, DeployMethodTypes::GIT);
    }

    private function processUpdate(Client $client, UpdateGitImageInput $input, array $inputData, string $updateType): void
    {
        $agentJobId = $this->updateGitImageOgAgentAction->__invoke($input, $client);
        
        if (!$agentJobId) {
            if ($input->queue) {
                $this->createService->__invoke($client, CommandTypes::UPDATE_IMAGE_GIT, TraceStatus::PENDING, null, $inputData);
            }
            return;
        }

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

    private function createInputData(UpdateGitImageInput $input, Client $client, $partition): array
    {
        return [
            'method' => 'ModificarImagenGit',
            'type' => 'git',
            'client' => $client->getUuid(),
            'diskNumber' => $partition->getDiskNumber(),
            'partitionNumber' => $partition->getPartitionNumber(),
            'repositoryName' => $input->gitRepository,
        ];
    }
} 