<?php

namespace App\Controller\OgRepository\Git;

use App\Controller\OgRepository\AbstractOgRepositoryController;
use App\Dto\Input\DeleteRepositoryInput;
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;

#[AsController]
class DeleteAction extends AbstractOgRepositoryController
{
    public function __invoke(ImageRepository $repository, DeleteRepositoryInput $input): JsonResponse
    {
        $content = $this->createRequest('DELETE', 'http://'.$repository->getIp().':8006/ogrepository/v1/git/repositories/'.$input->repository, [
        ]);

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

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