// JavaScript Document
// incomplete
exports.onesignal_createuser = async function (options) {

   // get appid
   const appid = this.parseRequired(options.appid, '*', 'No app ID entered')
   console.log(options.appid + " => " + appid)

   const identity = this.parseRequired(options.identity, '*', 'No identity specified')
   console.log(options.identity + " => " + identity)

   const pushtype = this.parseOptional(options.pushtype, '*', 'IOSPush')
   console.log(options.pushtype + " => " + pushtype)

   const token = this.parseOptional(options.token, '*', null)
   console.log(options.token + " => " + token)

   const tags = this.parseOptional(options.tags, '*', null)
   console.log(options.tags + " => " + tags)



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

   const url = `https://api.onesignal.com/apps/${appid}/users`;
   const os_options = {
      method: 'POST',
      headers: {
         accept: 'application/json', 'content-type': 'application/json',
         Authorization: `Basic ${process.env.ONESIGNAL_REST_API_KEY}`
      },
      body: JSON.stringify({
         properties: { tags: tags },
         subscriptions: [{
            type: pushtype,
            token: token,
            identity: identity,
            enabled: options.enable,
            notification_types: (options.enabled == true ? 1 : -99)
         }]
      })
   };

   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));

}