<?php

declare(strict_types=1);

namespace App\Controller\OgBoot;

use App\Service\OgBoot\StatusService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Attribute\Route;
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;

#[Route('/og-boot')]
#[AsController]
class OgBootController extends AbstractController
{
    public function __construct(
        private readonly StatusService $ogbootStatusService,
    )
    {
    }

    /**
     * @throws TransportExceptionInterface
     * @throws ServerExceptionInterface
     * @throws RedirectionExceptionInterface
     * @throws ClientExceptionInterface
     */
    #[Route('/status', name: 'ogboot_status',  methods: ['GET'])]
    public function status(): JsonResponse
    {
        $data = $this->ogbootStatusService->__invoke();

        if (isset($data['error']) && $data['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) {
            throw new BadRequestHttpException('An error occurred while fetching the status: ' . $data['details']);
        }

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