
#ifdef HAVE_CONFIG_H
#include "../../ext_config.h"
#endif

#include <php.h>
#include "../../php_ext.h"
#include "../../ext.h"

#include <Zend/zend_operators.h>
#include <Zend/zend_exceptions.h>
#include <Zend/zend_interfaces.h>

#include "kernel/main.h"
#include "kernel/fcall.h"
#include "kernel/memory.h"
#include "kernel/operators.h"
#include "kernel/object.h"
#include "kernel/array.h"


/**
 * This file is part of the Phalcon Framework.
 *
 * (c) Phalcon Team <team@phalcon.io>
 *
 * For the full copyright and license information, please view the LICENSE.txt
 * file that was distributed with this source code.
 *
 * Implementation of this component has been inspired by the queue-interop and
 * enqueue projects.
 *
 * @link    https://github.com/queue-interop/queue-interop
 * @license https://github.com/queue-interop/queue-interop/blob/master/LICENSE
 *
 * @link    https://github.com/php-enqueue/enqueue-dev
 * @license https://github.com/php-enqueue/enqueue-dev/blob/master/LICENSE
 */
/**
 * Maps an adapter name to its ConnectionFactory. Mirrors
 * Phalcon\Storage\AdapterFactory.
 *
 * @phpstan-import-type queue_connection_options from QueueTypes
 */
ZEPHIR_INIT_CLASS(Phalcon_Queue_AdapterFactory)
{
	ZEPHIR_REGISTER_CLASS_EX(Phalcon\\Queue, AdapterFactory, phalcon, queue_adapterfactory, phalcon_factory_abstractfactory_ce, phalcon_queue_adapterfactory_method_entry, 0);

	return SUCCESS;
}

/**
 * AdapterFactory constructor.
 *
 * @phpstan-param array<string, class-string<ConnectionFactoryInterface>> $services
 */
PHP_METHOD(Phalcon_Queue_AdapterFactory, __construct)
{
	zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
	zend_long ZEPHIR_LAST_CALL_STATUS;
	zval *services_param = NULL;
	zval services;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&services);
	ZEND_PARSE_PARAMETERS_START(0, 1)
		Z_PARAM_OPTIONAL
		ZEPHIR_Z_PARAM_ARRAY(services, services_param)
	ZEND_PARSE_PARAMETERS_END();
	ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);
	zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);
	zephir_fetch_params(1, 0, 1, &services_param);
	if (!services_param) {
		ZEPHIR_INIT_VAR(&services);
		array_init(&services);
	} else {
		zephir_get_arrval(&services, services_param);
	}
	ZEPHIR_CALL_METHOD(NULL, this_ptr, "init", NULL, 0, &services);
	zephir_check_call_status();
	ZEPHIR_MM_RESTORE();
}

/**
 * Creates a new ConnectionFactory for the named adapter.
 *
 * @phpstan-param queue_connection_options $options
 */
PHP_METHOD(Phalcon_Queue_AdapterFactory, newInstance)
{
	zend_class_entry *_1;
	zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
	zend_long ZEPHIR_LAST_CALL_STATUS;
	zval options;
	zval name_zv, *options_param = NULL, definition, _0;
	zend_string *name = NULL;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&name_zv);
	ZVAL_UNDEF(&definition);
	ZVAL_UNDEF(&_0);
	ZVAL_UNDEF(&options);
	ZEND_PARSE_PARAMETERS_START(1, 2)
		Z_PARAM_STR(name)
		Z_PARAM_OPTIONAL
		ZEPHIR_Z_PARAM_ARRAY(options, options_param)
	ZEND_PARSE_PARAMETERS_END();
	ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);
	zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);
	if (ZEND_NUM_ARGS() > 1) {
		options_param = ZEND_CALL_ARG(execute_data, 2);
	}
	zephir_memory_observe(&name_zv);
	ZVAL_STR_COPY(&name_zv, name);
	if (!options_param) {
		ZEPHIR_INIT_VAR(&options);
		array_init(&options);
	} else {
		zephir_get_arrval(&options, options_param);
	}
	ZEPHIR_CALL_METHOD(&definition, this_ptr, "getservice", NULL, 0, &name_zv);
	zephir_check_call_status();
	zephir_fetch_safe_class(&_0, &definition);
	_1 = zephir_fetch_class_str_ex(Z_STRVAL_P(&_0), Z_STRLEN_P(&_0), ZEND_FETCH_CLASS_AUTO);
	if(!_1) {
		RETURN_MM_NULL();
	}
	object_init_ex(return_value, _1);
	ZEPHIR_LAST_CALL_STATUS = zephir_check_constructor_access(return_value);
	zephir_check_call_status();
	if (zephir_has_constructor(return_value)) {
		ZEPHIR_CALL_METHOD(NULL, return_value, "__construct", NULL, 0, &options);
		zephir_check_call_status();
	}

	RETURN_MM();
}

/**
 * Returns the exception class for the factory
 *
 * @return class-string<\Throwable>
 */
PHP_METHOD(Phalcon_Queue_AdapterFactory, getExceptionClass)
{

	RETURN_STRING("Phalcon\\Queue\\Exceptions\\Exception");
}

/**
 * Returns the available adapters.
 *
 * @return string[]
 */
PHP_METHOD(Phalcon_Queue_AdapterFactory, getServices)
{

	zephir_create_array(return_value, 4, 0);
	add_assoc_stringl_ex(return_value, SL("beanstalk"), SL("Phalcon\\Queue\\Adapter\\Beanstalk\\BeanstalkConnectionFactory"));
	add_assoc_stringl_ex(return_value, SL("memory"), SL("Phalcon\\Queue\\Adapter\\Memory\\MemoryConnectionFactory"));
	add_assoc_stringl_ex(return_value, SL("redis"), SL("Phalcon\\Queue\\Adapter\\Redis\\RedisConnectionFactory"));
	add_assoc_stringl_ex(return_value, SL("stream"), SL("Phalcon\\Queue\\Adapter\\Stream\\StreamConnectionFactory"));
	return;
}

