<?php

namespace App\Controller\OgBoot\PxeBootFile;

use App\Controller\OgBoot\AbstractOgBootController;
use App\Entity\Client;
use App\Entity\PxeTemplate;
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;

#[AsController]
class PostAction extends AbstractOgBootController
{
    /**
     * @throws TransportExceptionInterface
     * @throws ServerExceptionInterface
     * @throws RedirectionExceptionInterface
     * @throws ClientExceptionInterface
     */
    public function __invoke(Client $client, PxeTemplate $pxeTemplate): JsonResponse
    {
        $ogRepoIp = $this->ogBootIp;

        $ogRepoIp = $client->getRepository()?->getIp()
            ?? $client->getOrganizationalUnit()?->getNetworkSettings()?->getRepository()?->getIp();

        $ogLive = $client->getOgLive()?->getFilename()
            ?? $client->getOrganizationalUnit()?->getNetworkSettings()?->getOgLive()?->getFilename();

        $params = [
            'json' => [
                'template_name' => $pxeTemplate->getName(),
                'mac' => strtolower($client->getMac()),
                'lang' => 'es_ES.UTF-8',
                'ip' => $client->getIp(),
                'server_ip' => $this->ogBootIp,
                'server_api_port' => $this->ogBootApiPort,
                'server_pxe_port' => $this->ogBootPxePort,
                'router' =>  $client->getOrganizationalUnit()->getNetworkSettings()?->getRouter(),
                'netmask' => $client->getOrganizationalUnit()->getNetworkSettings() ? $client->getOrganizationalUnit()->getNetworkSettings()->getNetmask() : '255.255.255.0',
                'computer_name' => $client->getName(),
                'netiface' => $client->getNetiface() ? $client->getNetiface() : $client->getOrganizationalUnit()->getNetworkSettings()?->getNetiface(),
                'group' => $client->getOrganizationalUnit()->getName(),
                'ogrepo' => $ogRepoIp,
                'ogcore' => $this->ogCoreIP,
                'oglive' => $this->ogBootIp,
                'oglog' => $this->ogLogIp,
                'ogshare' => $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare()
                    ? $client->getOrganizationalUnit()->getNetworkSettings()?->getOgShare(): $this->ogBootIp,
                'oglivedir' => $ogLive,
                'ogprof' => 'false',
                'hardprofile' => $client->getHardwareProfile() ? $client->getHardwareProfile()->getDescription() : 'default',
                'ogntp' => $client->getOrganizationalUnit()->getNetworkSettings()?->getNtp(),
                'ogdns' => $client->getOrganizationalUnit()->getNetworkSettings()?->getDns(),
                'ogProxy' => $client->getOrganizationalUnit()->getNetworkSettings()?->getProxy(),
                'resolution' => $client->getResolution() ?? '791'
            ]
        ];

        $content = $this->createRequest('POST', '/ogboot/v1/pxes', $params);

        $client->setPxeSync(true);
        $this->entityManager->persist($client);
        $this->entityManager->flush();

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