<?php
// src/OgGitBundle/Service/OgGitService.php

namespace App\OgGitBundle\Service;
use Exception;
use Psr\Log\LoggerInterface;
use GuzzleHttp\Client;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class OgGitService
{
    private $client;
    private $ogGitUrl;
    public function __construct(ParameterBagInterface $params)
    {
        // Obtener la URL de la variable de entorno OGGIT_URL
        $this->ogGitUrl = $params->get('OGGIT_URL');
        $this->client = new Client();
    }
    public function convertMaskToCIDR($mask)
    {
        $bits = 0;
        $mask = explode(".", $mask);

        foreach ($mask as $octect)
            $bits += strlen(str_replace("0", "", decbin($octect)));

        return $bits;
    }

    public function isValidBranchName($name) {
        $output = null;
        $return_var = null;
        exec("git check-ref-format --branch " . escapeshellarg($name), $output, $return_var);
        # $output = exec("python3 ./gitlib.py " . escapeshellarg($name), $output, $return_var);
        return $return_var === 0;
    }
    
    public function makeRequest(string $method, string $endpoint, array $params = [])
    {
        $url = $this->ogGitUrl . $endpoint;

        try {
            $response = $this->client->request($method, $url, $params);
            $body = $response->getBody();
            return json_decode($body, true);
        } catch (\Exception $e) {
            return ['error' => 'No se pudo conectar con el servicio Python: ' . $e->getMessage()];
        }
    }



}
