<?php

namespace App\Service\OgDhcp;

use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

readonly class StatusService
{
    public function __construct(
        #[Autowire(env: 'OG_DHCP_API_URL')]
        private string $ogDhcpApiUrl
    )
    {
    }

    /**
     * @throws TransportExceptionInterface
     * @throws ServerExceptionInterface
     * @throws RedirectionExceptionInterface
     * @throws ClientExceptionInterface
     */
    public function __invoke()
    {
        $httpClient = HttpClient::create([
            'verify_peer' => false,
            'verify_host' => false,
        ]);

        try {
            $response = $httpClient->request('GET', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/status', [
                'headers' => [
                    'accept' => 'application/json',
                ],
            ]);
        } catch (TransportExceptionInterface $e) {
            return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR);
        }

        return json_decode($response->getContent(), true);
    }
}