<?php

namespace App\Controller\OgDhcp\Subnet;

use App\Controller\OgDhcp\AbstractOgDhcpController;
use App\Dto\Input\SubnetAddHostInput;
use App\Entity\Client;
use App\Entity\Subnet;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

#[AsController]
class PutHostAction extends AbstractOgDhcpController
{
    /**
     * @throws TransportExceptionInterface
     * @throws ServerExceptionInterface
     * @throws RedirectionExceptionInterface
     * @throws ClientExceptionInterface
     */
    public function __invoke(string $mac, Client $client): JsonResponse
    {
        $subnet = $client->getSubnet();

        /** @var Client $clientEntity */
        $data = [
            'hostname' => $client->getName(),
            'oldMacAddress' => strtolower($mac),
            'macAddress' => strtolower($client->getMac()),
            'address' => $client->getIp(),
        ];

        $params = [
            'json' => $data
        ];

        $content = $this->createRequest('PUT', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$subnet->getServerId().'/hosts', $params);

        return new JsonResponse(status: Response::HTTP_OK);
    }
}