meta {
  name: Wakapi API Tests
  seq: 1
}

auth {
  mode: none
}

vars:pre-request {
  BASE_URL: http://localhost:3000
  READUSER_API_KEY: 33e7f538-0dce-4eba-8ffe-53db6814ed42
  WRITEUSER_API_KEY: f7aa255c-8647-4d0b-b90f-621c58fd580f
  TZ: Europe/Berlin
  TZ_OFFSET: +02:00
  READUSER_PASSWORD: testpw
  WRITEUSER_PASSWORD: testpw
}

script:pre-request {
  const moment = require('moment')
  
  // pretend we're in Berlin, as this is also the time zone configured for the user
  const userZone = 'Europe/Berlin'
  
  // https://stackoverflow.com/a/63199512
  // return UTC offset of timezone in minutes
  function getTimezoneOffset(timeZone, date = new Date()) {
    const tz = date.toLocaleString("en", {timeZone, timeStyle: "long"}).split(" ").slice(-1)[0];
    const dateString = date.toString();
    const offset = Date.parse(`${dateString} UTC`) - Date.parse(`${dateString} ${tz}`);
    return offset / 1000 / 60;
  }
  
  const utcOffset = getTimezoneOffset(userZone)
  bru.setVar('utcOffset', utcOffset)
  
  const now = moment().utcOffset(utcOffset)
  const startOfDay = now.clone().startOf('day')
  const endOfDay = now.clone().endOf('day')
  const endOfTomorrow = now.clone().add(1, 'd').endOf('day')
  const startOfYesterday = now.clone().add(-1, 'day').startOf('day')
  const endOfYesterday = now.clone().add(-1, 'day').endOf('day')
  
  // Auth stuff
  const readApiKey = bru.getCollectionVar('READUSER_API_KEY')
  const writeApiKey = bru.getCollectionVar('WRITEUSER_API_KEY')
  
  if (!readApiKey || !writeApiKey) {
      throw new Error('no api key given')
  }
  
  bru.setVar('READUSER_TOKEN', base64encode(readApiKey))
  bru.setVar('WRITEUSER_TOKEN', base64encode(writeApiKey))
  
  function base64encode(str) {
      return Buffer.from(str, 'utf-8').toString('base64')
  }
  
  // Heartbeat stuff
  bru.setVar('tsNow', now.clone().format('x') / 1000)
  bru.setVar('tsNowMinus1Min', now.clone().add(-1, 'm').format('x') / 1000)
  bru.setVar('tsNowMinus2Min', now.clone().add(-2, 'm').format('x') / 1000)
  bru.setVar('tsNowMinus3Min', now.clone().add(-3, 'm').format('x') / 1000)
  bru.setVar('tsStartOfDay', startOfDay.format('x') / 1000)
  bru.setVar('tsLateNightToday', startOfDay.clone().add(22, 'h').format('x') / 1000)
  bru.setVar('tsEndOfDay', endOfDay.format('x') / 1000)
  bru.setVar('tsStartOfYesterday', startOfYesterday.format('x') / 1000)
  bru.setVar('tsLateNightYesterday', startOfYesterday.clone().add(22, 'h').format('x') / 1000)
  bru.setVar('tsEndOfYesterday', endOfYesterday.format('x') / 1000)
  bru.setVar('tsEndOfTomorrow', endOfTomorrow.format('x') / 1000)
  bru.setVar('tsStartOfDayIso', startOfDay.toISOString())
  bru.setVar('tsEndOfDayIso', endOfDay.toISOString())
  bru.setVar('tsEndOfTomorrowIso', endOfTomorrow.toISOString())
  bru.setVar('ts1', now.clone().startOf('hour').format('x') / 1000)
  bru.setVar('ts2', now.clone().startOf('hour').add(1, 'm').format('x') / 1000)
  bru.setVar('ts3', now.clone().startOf('hour').add(2, 'm').format('x') / 1000)
  bru.setVar('ts4', now.clone().add(-1, 'months').format('x') / 1000)  // last month
  bru.setVar('todayDate', startOfDay.format('YYYY-MM-DD'))
  bru.setVar('todayDateIso', startOfDay.toISOString(true))
  bru.setVar('yesterdayDate', startOfYesterday.format('YYYY-MM-DD'))
  bru.setVar('yesterdayDateIso', startOfYesterday.toISOString(true))
}
