const config = require('/pb_hooks/seed/config.js');
const utils = require('/pb_hooks/seed/utils.js');

module.exports = $app => {
  const { getAsset } = utils;
  const { id_v, id_c, cat_id_tech } = config;

  let row_datas = [
    ['1'.padStart(15, 0), getAsset('keyboard.jpg'), getAsset('keyboard.mp3'), 'keyboard', '鍵盤', 'sample_e', 'sample_c', cat_id_tech, '', id_v],
    ['2'.padStart(15, 0), getAsset('mouse.jpg'), getAsset('mouse.mp3'), 'mouse', '滑鼠', 'sample_e mouse', 'sample_c mouse', cat_id_tech, '', id_v],
  ];

  dirtyTruncateTable('Vocabularies');

  for (let i = 0; i < row_datas.length; i++) {
    let vocabularies_collection = $app.findCollectionByNameOrId('Vocabularies');
    let data = row_datas[i];

    let record = new Record(vocabularies_collection);
    record.set('id', data[0]);
    record.set('image', data[1]);
    record.set('sound', data[2]);
    record.set('word', data[3]);
    record.set('word_c', data[4]);
    record.set('sample_e', data[5]);
    record.set('sample_c', data[6]);
    record.set('cat_id', data[7]);
    record.set('category', data[8]);
    record.set('lesson_type_id', data[9]);

    $app.save(record);
  }
  console.log('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();
};
