<?php

namespace App\Controller\OgRepository\Git;

use App\Controller\OgRepository\AbstractOgRepositoryController;
use App\Dto\Input\CreateTagInput;
use App\Entity\ImageRepository;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

#[AsController]
class CreateTagAction extends AbstractOgRepositoryController
{
    public function __invoke(ImageRepository $repository, CreateTagInput $input): JsonResponse
    {
        $content = $this->createRequest('POST', 'http://'.$repository->getIp().':8006/ogrepository/v1/git/repositories/'.$input->repository.'/tags', [
            'json' => [
                'commit' => $input->commit,
                'message' => $input->message,
                'name' => $input->name
            ]
        ]);

        if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) {
            throw new BadRequestHttpException('Error creating tag');
        }

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