<?php

declare(strict_types=1);

namespace App\Controller\OgRepository;

use App\Controller\OgRepository\Image\CreateAuxFilesAction;
use App\Controller\OgRepository\Image\GetAction;
use App\Service\Trace\CreateService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
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\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 AbstractOgRepositoryController extends AbstractController
{
    public function __construct(
        protected readonly  EntityManagerInterface   $entityManager,
        protected readonly  HttpClientInterface      $httpClient,
        protected readonly  CreateService            $createService,
        protected readonly  LoggerInterface          $logger,
    )
    {
    }

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

        $this->logger->info('Sending HTTP request', [
            'method' => $method,
            'url' => $url,
            'params' => $params
        ]);

        try {
            $response = $this->httpClient->request($method, $url, $params);
            $content = $response->getContent();
            $decodedContent = json_decode($content, true);
            
            $this->logger->info('HTTP response received successfully', [
                'method' => $method,
                'url' => $url,
                'status_code' => $response->getStatusCode(),
                'response_size' => strlen($content),
                'decoded_content' => $decodedContent
            ]);
            
            return $decodedContent;
        } catch (ClientExceptionInterface | ServerExceptionInterface $e) {
            $statusCode = $e->getResponse()?->getStatusCode() ?? Response::HTTP_INTERNAL_SERVER_ERROR;
            
            $this->logger->error('Client/server error in HTTP request', [
                'method' => $method,
                'url' => $url,
                'error' => $e->getMessage(),
                'status_code' => $statusCode,
                'response_content' => $e->getResponse()?->getContent(false)
            ]);

            return [
                'code' => $statusCode,
                'error' => $e->getMessage(),
                'details' => $e->getResponse()?->getContent(false) ?? $e->getMessage(),
            ];
        } catch (TransportExceptionInterface $e) {
            $this->logger->error('Transport error in HTTP request', [
                'method' => $method,
                'url' => $url,
                'error' => $e->getMessage()
            ]);

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

}
