// SIGNL4 Webhook
try {
    var response,
        payload,
        params = JSON.parse(value),
        endpoint = 'https://connect.signl4.com/webhook/',
        request = new HttpRequest();

    if (typeof params.HTTPProxy === 'string' && params.HTTPProxy.trim() !== '') {
        request.setProxy(params.HTTPProxy);
    }

    if (typeof params.teamsecret === 'string' && params.teamsecret.trim() !== '') {
        endpoint += params.teamsecret;
        delete params.teamsecret;
    }
    else {
        throw 'The team secret of your SIGNL4 team cannot be empty.';
    }

    if (typeof params.Severity === 'string' && params.Severity === '{EVENT.SEVERITY}') {
        params.Severity = 'Not classified';
    }

	if (typeof params.User === 'string' && params.User === '{USER.FULLNAME}') {
        params.User = '';
    }

	if (typeof params.Event_Update_Action === 'string' && params.Event_Update_Action === '{EVENT.UPDATE.ACTION}') {
        params.Event_Update_Action = '';
    }

	// Assemble X-S4-ExternalID for two-way integration
	// Format: "ZabbixEventID: 222 ZabbixURL: https://your-zabbix-server/zabbix/"
	params['X-S4-ExternalID'] = 'ZabbixEventID: ' + params.Event_ID;
	if (typeof params.Zabbix_URL === 'string' && params.Zabbix_URL.indexOf('http') == 0) {
		// Make sure the URL ends with '/'
		if (params.Zabbix_URL.charAt(params.Zabbix_URL.length - 1) != '/') {
			params.Zabbix_URL = params.Zabbix_URL + '/';
		}

		params['X-S4-ExternalID'] = params['X-S4-ExternalID'] + ' ZabbixURL: ' +  params.Zabbix_URL;

		// Add Link parameter
		params['Link'] = params.Zabbix_URL + "tr_events.php?triggerid="+params.Trigger_ID + "&eventid=" + params.Event_ID;
	}

	// Check if this is a new problem or a recovery
	if (params.Trigger_Status == 'OK') {
		params['X-S4-Status'] = 'resolved';
	}
	else {
		params['X-S4-Status'] = 'new';
		params['X-S4-SourceSystem'] = 'Zabbix';
	}

    payload = JSON.stringify(params);
    Zabbix.log(4, '[ SIGNL4 Webhook ] Sending request: ' + payload);

    request.addHeader('Content-Type: application/json');
    response = request.post(endpoint, 'payload=' + payload);

    Zabbix.log(4, '[ SIGNL4 Webhook ] Received response with status code ' +
            request.getStatus() + '\\n' + response
    );

    if (request.getStatus() !== 201) {
        throw 'Request failed with status code ' + request.getStatus() +
                '. Check debug log for more information.';
    }

    return 'OK';
}
catch (error) {
    Zabbix.log(4, '[ SIGNL4 Webhook ] ERROR: ' + error);

    throw 'Sending failed: ' + error;
}