<?php

if (!function_exists('api_url')) {
    /**
     * Generar URL completa para un endpoint de la API
     *
     * @param string $endpoint Clave del endpoint (ej: 'business.create_user')
     * @param array $params Parámetros para reemplazar en la URL (ej: ['id' => '123'])
     * @return string
     */
    function api_url(string $endpoint, array $params = []): string
    {
        $baseUrl = config('api.base_url');
        $endpointPath = config("api.{$endpoint}");
        
        if (!$endpointPath) {
            throw new InvalidArgumentException("Endpoint '{$endpoint}' no encontrado en la configuración");
        }
        
        // Reemplazar parámetros en la URL
        foreach ($params as $key => $value) {
            $endpointPath = str_replace("{{$key}}", $value, $endpointPath);
        }
        
        return $baseUrl . $endpointPath;
    }
}

if (!function_exists('api_headers')) {
    /**
     * Obtener headers por defecto para las llamadas a la API
     *
     * @param array $additionalHeaders Headers adicionales
     * @return array
     */
    function api_headers(array $additionalHeaders = []): array
    {
        $defaultHeaders = config('api.headers', []);
        return array_merge($defaultHeaders, $additionalHeaders);
    }
}

if (!function_exists('api_config')) {
    /**
     * Obtener configuración específica de la API
     *
     * @param string $key Clave de configuración
     * @param mixed $default Valor por defecto
     * @return mixed
     */
    function api_config(string $key, mixed $default = null): mixed
    {
        return config("api.{$key}", $default);
    }
}

if (!function_exists('should_log_api_requests')) {
    /**
     * Verificar si se deben loguear las requests de la API
     *
     * @return bool
     */
    function should_log_api_requests(): bool
    {
        return config('api.error_handling.log_requests', true);
    }
}

if (!function_exists('should_log_api_responses')) {
    /**
     * Verificar si se deben loguear las responses de la API
     *
     * @return bool
     */
    function should_log_api_responses(): bool
    {
        return config('api.error_handling.log_responses', true);
    }
}

if (!function_exists('should_log_api_errors')) {
    /**
     * Verificar si se deben loguear los errores de la API
     *
     * @return bool
     */
    function should_log_api_errors(): bool
    {
        return config('api.error_handling.log_errors', true);
    }
}