<?php

/*
|--------------------------------------------------------------------------
| CONFIGURACION VICIDIAL / ASTERISK
|--------------------------------------------------------------------------
*/

define('VICI_URL',      'http://TU_IP_VICIDIAL');   // Ej: http://192.168.1.100
define('VICI_USER',     'admin');
define('VICI_PASS',     'admin');
define('VICI_SOURCE',   'crm');

define('VICI_DEFAULT_CAMPAIGN', 'CAMP1');
define('VICI_DEFAULT_LIST',     '998');

/*
|--------------------------------------------------------------------------
| FUNCION BASE: LLAMAR A LA API DE VICIDIAL
|--------------------------------------------------------------------------
*/

function vicidial_api(array $params, bool $non_agent = false): array
{

    $endpoint = $non_agent
        ? VICI_URL . '/vicidial/non_agent_api.php'
        : VICI_URL . '/agc/api.php';

    $params['source'] = VICI_SOURCE;
    $params['user']   = VICI_USER;
    $params['pass']   = VICI_PASS;

    $url = $endpoint . '?' . http_build_query($params);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $response = curl_exec($ch);
    $error    = curl_error($ch);
    curl_close($ch);

    if ($error) {
        return ['success' => false, 'error' => $error];
    }

    return ['success' => true, 'response' => trim($response)];

}