<?php

include 'api.php';

// ARGUMENTOS: USER PASSWORD PBX
if (isset($argv[1]) && isset($argv[2])) {

    $data = [
        "id_user" => $magnusBilling->getId('user', 'username', $argv[1]),
        "name" => $argv[1],
        "callerid" => $argv[1],
        "secret" => $argv[2]
    ];

    if(isset($argv[3])) {
        $data["host"] = $argv[3];
        $data["insecure"] = "invite,port";
    }

    $sip_id = $magnusBilling->getId('sip', 'name', $argv[1]);
    
    if ($sip_id == null) {
        $result = $magnusBilling->create('sip', $data);
    } else {
        $result = $magnusBilling->update('sip', $sip_id, $data);
    }


    if ($result['success'] == 1) {
        print("[201] OK id=" . $result['rows'][0]['id']);
    } else {
        print_r($result);
    }
    
} else {
    print("[404] ERROR No se han introducido todos los parámetros necesarios.");
}
