module.exports = $app => {
  const ASSETS_DIR = '/pb_hooks/assets';
  const getAsset = name => $filesystem.fileFromPath(ASSETS_DIR + '/' + name);
  const id_v = '1'.padStart(15, 0); //id_vocabulary
  const id_c = '2'.padStart(15, 0); //id_connectives

  let row_array = [
    ['1'.padStart(15, 0), 'news', '', getAsset('ci_news.jpg'), 1, 'visible', id_v, 'desc', 'remarks'],
    ['2'.padStart(15, 0), 'sports', '', getAsset('ci_sports.jpg'), 1, 'visible', id_v, 'desc', 'remarks'],
    ['3'.padStart(15, 0), 'technology', '', getAsset('ci_technology.jpg'), 1, 'visible', id_v, 'desc', 'remarks'],
    ['4'.padStart(15, 0), 'art', '', getAsset('ci_art.jpg'), 1, 'visible', id_v, 'desc', 'remarks'],
    ['5'.padStart(15, 0), 'basic', '', getAsset('ci_basic.jpg'), 1, 'visible', id_v, 'desc', 'remarks'],
    ['6'.padStart(15, 0), 'nature', '', getAsset('ci_nature.jpg'), 1, 'visible', id_v, 'desc', 'remarks'],
    ['7'.padStart(15, 0), 'workplace', '', getAsset('ci_workplace.jpg'), 1, 'visible', id_v, 'desc', 'remarks'],
    ['99'.padStart(15, 0), 'connectives', '', getAsset('1.png'), 1, 'visible', id_c, 'desc', 'remarks'],
    // ["2".padStart(15, 0), "Connectives", "connectivse", 1, "visible"],
    // ["3".padStart(15, 0), "Testing visible", "testing", 3, "visible"],
    // ["4".padStart(15, 0), "Testing hidden", "testing", 3, "hidden"],
  ];
  dirtyTruncateTable('Categories');

  let lt_collection = $app.findCollectionByNameOrId('Categories');

  for (let i = 0; i < row_array.length; i++) {
    let lesson_type = row_array[i];

    let record = new Record(lt_collection);
    record.set('id', lesson_type[0]);
    record.set('cat_name', lesson_type[1]);
    record.set('cat_image_url', lesson_type[2]);
    record.set('cat_image', lesson_type[3]);
    record.set('pos', lesson_type[4]);
    record.set('visible', lesson_type[5]);
    record.set('lesson_id', lesson_type[6]);
    record.set('description', lesson_type[7]);
    record.set('remarks', lesson_type[8]);

    $app.save(record);
  }

  console.log('seeding done');
};

const dirtyTruncateTable = COLLECTION_NAME => {
  console.log(`perform dirty method to truncate table "${COLLECTION_NAME}"`);
  const cmd_to_exec = $os.cmd('sqlite3', '/pb_data/data.db', `DELETE from ${COLLECTION_NAME};`);
  cmd_to_exec.output();
};
