<?php

namespace App\Controller\OgRepository\Git;

use App\Controller\OgRepository\AbstractOgRepositoryController;
use App\Entity\Client;
use App\Entity\ImageRepository;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
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 SshKeyAction extends AbstractOgRepositoryController
{
    /**
     * @throws TransportExceptionInterface
     * @throws ServerExceptionInterface
     * @throws RedirectionExceptionInterface
     * @throws ClientExceptionInterface
     */
    public function __invoke(ImageRepository $repository, Client $client): JsonResponse
    {
        $content = $this->createRequest('POST', 'http://'.$repository->getIp().':8006/ogrepository/v1/git/ssh_key', [
            'json' => [
                'description' => $client->getName()->getOglive()->getName(),
                'oglive' => $client->getOglive()->getDirectory().'.iso',
            ],
        ]);

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