<?php

declare(strict_types=1);

namespace App\Controller\OgBoot;

use App\Controller\OgBoot\PxeBootFile\PostAction;
use App\Service\Trace\CreateService;
use App\Service\Utils\ExtractOgLiveFilenameDateService;
use App\Service\Utils\SimplifyOgLiveFilenameService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Exception\HttpException;
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]
abstract class AbstractOgBootController extends AbstractController
{
    public function __construct(
        #[Autowire(env: 'OG_BOOT_IP')]
        protected string                                    $ogBootIp,
        #[Autowire(env: 'OG_BOOT_PXE_PORT')]
        protected string                                    $ogBootPxePort,
        #[Autowire(env: 'OG_BOOT_API_PORT')]
        protected string                                    $ogBootApiPort,
        #[Autowire(env: 'OG_CORE_IP')]
        protected string                                    $ogCoreIP,
        #[Autowire(env: 'OG_LOG_IP')]
        protected string                                    $ogLogIp,
        protected readonly EntityManagerInterface           $entityManager,
        protected readonly HttpClientInterface              $httpClient,
        protected readonly CreateService                    $createService,
        protected readonly SimplifyOgLiveFilenameService    $simplifyOgLiveFilenameService,
        protected readonly ExtractOgLiveFilenameDateService $extractOgLiveFilenameDateService,
        protected readonly  LoggerInterface                 $logger,

    )
    {
    }

    /**
     * @throws TransportExceptionInterface
     * @throws ServerExceptionInterface
     * @throws RedirectionExceptionInterface
     * @throws ClientExceptionInterface
     */
    public function createRequest (string $method, string $url, array $params = []): JsonResponse|array
    {
        $params = array_merge($params, [
            'headers' => [
                'accept' => 'application/json',
                'Content-Type' => 'application/json'
            ],
        ]);

        try {
            $response = $this->httpClient->request($method, 'http://'.$this->ogBootIp.':'.$this->ogBootApiPort.$url, $params);

            return json_decode($response->getContent(), true);
        } catch (ClientExceptionInterface | ServerExceptionInterface $e) {
            $this->logger->error(sprintf('Client/Server error in request to %s: %s', $url, $e->getMessage()));

            return [
                'code' => Response::HTTP_INTERNAL_SERVER_ERROR,
                'error' => 'Client/Server error',
                'details' => $e->getMessage(),
            ];
        } catch (TransportExceptionInterface $e) {
            $this->logger->error(sprintf('Transport error in request to %s: %s', $url, $e->getMessage()));

            return [
                'code' => Response::HTTP_INTERNAL_SERVER_ERROR,
                'error' => 'Transport error',
                'details' => $e->getMessage(),
            ];
        }
    }
}
