const fetch = require('node-fetch')

const SUPA = process.env.SUPA_URL || 'http://192.168.1.62:18000'
const KEY = process.env.SUPA_KEY || process.env.SERVICE_ROLE_KEY || ''

const payload = [
  {
    cid: 'CID_TEST_001',
    user_id: 'a8e3a568-fc1f-4237-bcc5-5722e2fca0a3',
    platform: 'android',
    active: true
  }
]

(async () => {
  try {
    const url = `${SUPA.replace(/\/$/, '')}/rest/v1/push_devices`
    console.log('POST', url)
    const resp = await fetch(url, {
      method: 'POST',
      headers: {
        apikey: KEY,
        Authorization: `Bearer ${KEY}`,
        'Content-Type': 'application/json',
        Prefer: 'return=representation'
      },
      body: JSON.stringify(payload),
      timeout: 15000
    })
    console.log('status', resp.status)
    const txt = await resp.text()
    console.log('body:', txt)
  } catch (e) {
    console.error('insert error', e && e.message ? e.message : e)
    process.exit(1)
  }
})()
