/*
 * Copyright (C) Sapphirecode - All Rights Reserved
 * This file is part of appreports which is released under GPL-3.0-or-later.
 * See file 'LICENSE' for full license details.
 * Created by Timo Hocker <timo@scode.ovh>, August 2020
 */

'use strict';

const faker = require ('faker');
const sn = require ('simplex-noise');

const apps = [];

async function create_app (knex) {
  const [ id ] = await knex ('app')
    .insert (
      { name: faker.random.word () }
    );
  apps.push (id);
}

function create_log (index, simplex) {
  const data = {
    num1: faker.random.number (),
    num2: simplex.noise2D (index * 0.1, 0),
    num3: simplex.noise2D (index * 0.1, 1000)
  };
  return {
    app_id:       faker.random.arrayElement (apps),
    message:   faker.random.words (),
    data:      JSON.stringify (data),
    timestamp: faker.date.recent ()
  };
}

async function seed (knex) {
  // eslint-disable-next-line no-console
  console.log ('creating seeds');
  for (let i = 0; i < 5; i++)
    // eslint-disable-next-line no-await-in-loop
    await create_app (knex);

  const simplex = (new sn);
  const log = (Array (20))
    .fill (() => null)
    .map ((a, index) => create_log (index, simplex));

  await knex.batchInsert ('log', log, 10);
}

module.exports = { seed };