// Node integration with wappler.io
// v1.0.0 - CURRENTLY UNTESTED
exports.onesignal_notification_history_sent = async function (options) {

    // get appid parsed as options.appid, throw error is missing
    const appid = this.parseRequired(options.appid, '*', 'No app ID entered')
    // log before and after vlaues
    console.log(options.appid + " => " + appid)

    var notification_id = this.parseRequired(options.notification_id, '*', 'Notification ID not specified')
    console.log(options.notification_id + " => " + notification_id)

    var to_email = this.parseRequired(options.email, '*', 'eMail not specified')
    console.log(options.email + " => " + to_email)

    const fetch = require('node-fetch');

    const url = `https://api.onesignal.com/notifications/${notification_id}/history`;
    const os_options = {
        method: 'POST',
        headers: { accept: 'application/json', 'content-type': 'application/json', Authorization: `Basic ${process.env.ONESIGNAL_REST_API_KEY}` },
        body: JSON.stringify({ app_id: appid, events: 'sent', email: to_email })
    };
    return fetch(url, os_options)
        .then(res => {

            return res.json().then(json => {
                json.status = res.status; // Add status to json response
                return json;
            });
        })
        .catch(err => console.error('Error:', err));
}