// JavaScript Document
exports.onesignal_create_push_notification = async function (options) {
   // get appid
   const appid = this.parseOptional(options.appid, '*', process.env.ONESIGNAL_APP_ID)
   console.log(options.appid + " => " + appid)
   // get message internal name
   const osname = this.parseOptional(options.osname, '*', 'message_auto')
   console.log(options.osname + " => " + osname)
   // target_channel
   const target_channel = this.parseOptional(options.target_channel, '*', 'push')
   console.log(options.target_channel + " => " + target_channel)
   // get language
   const oslang = this.parseOptional(options.oslang, '*', 'en')
   console.log(options.oslang + " => " + oslang)

   const segment = this.parseOptional(options.segment, '*', 'All')
   console.log("Segment: " + options.segment + " => " + segment)
   // gte message
   //const osmessage = this.parseOptional(options.os_message, '*', 'Hello world')
   //console.log(options.os_message + " => " + osmessage)

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

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

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

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

   const messages = this.parseRequired(options.bindings, '*', 'Bindings Required')
   console.log(options.bindings + " => " + messages)

   const headings = this.parseRequired(options.headings, '*', 'Headings Required')
   console.log(options.headings + " => " + headings)

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

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

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

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

   const filters = this.parseOptional(options.filters, '*', {})
   console.log(options.filters + " => " + filters)

   //const include_aliases = this.parseOptional(options.include_aliases, '*', '{ external_id: [] }')
   //console.log(options.include_aliasess + " => " + include_aliases)

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

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

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

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

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

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

   const url = 'https://api.onesignal.com/notifications?c=push';
   const os_options = {
      method: 'POST',
      headers: {
         accept: 'application/json', 'content-type': 'application/json',
         Authorization: `Basic ${process.env.ONESIGNAL_REST_API_KEY}`
      },
      body: JSON.stringify(
         Object.fromEntries(
            Object.entries({
               contents: messages,
               headings: headings, app_id: appid,
               included_segments: [segment],
               target_channel: target_channel,
               big_picture: osimage,
               chrome_big_picture: osimage,
               chrome_web_image: osimage,
               chrome_icon: osimage,
               chrome_web_icon: osimage,
               firefox_icon: osimage

            }).filter(([_, v]) => v != null)
         ))
   };
   console.log(os_options)
   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));

}
