<?php

namespace App\Controller\OgAgent;

use App\Controller\OgRepository\Git\CreateRepositoryAction;
use App\Controller\OgRepository\Git\SshKeyAction;
use App\Service\CreatePartitionService;
use App\Service\Trace\CreateService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
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;
use Symfony\Contracts\HttpClient\HttpClientInterface;

#[AsController]
abstract class AbstractOgAgentController extends AbstractController
{
    public function __construct(
        protected readonly  EntityManagerInterface   $entityManager,
        protected readonly  HttpClientInterface      $httpClient,
        protected readonly  CreatePartitionService   $createPartitionService,
        protected readonly  LoggerInterface          $logger,
        protected readonly  CreateService            $createService,
        protected readonly  CreateRepositoryAction   $createRepositoryAction,
        protected readonly  SshKeyAction             $sshKeyAction,
        #[Autowire(env: 'SSL_ENABLED')]
        private readonly string                      $sslEnabled,
    )
    {
    }

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


        if ($this->sslEnabled === 'true') {
            $params['verify_peer'] = true;
            $params['verify_host'] = false;
            $params['cafile'] = '/opt/opengnsys/ogcore/etc/certificates/ca.crt';
            $params['local_cert'] = '/opt/opengnsys/ogcore/etc/certificates/ogcore.crt';
            $params['local_pk'] = '/opt/opengnsys/ogcore/etc/certificates/ogcore.key';
        }

        try {
            $response = $this->httpClient->request($method, $url, $params);
            return json_decode($response->getContent(), true);
        } catch (ClientExceptionInterface | ServerExceptionInterface $e) {
            if ($e->getResponse() && $e->getResponse()->getStatusCode() === 409) {
                $this->logger->info('Client is busy (409 Conflict)', [
                    'url' => $url,
                    'status_code' => 409
                ]);
                
                return [
                    'code' => 409,
                    'message' => 'Client is busy',
                    'details' => $e->getMessage(),
                ];
            }
            
            $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(),
            ];
        }
    }
}