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
  const getId = (id) => id.padStart(15, 0);
  let row_array = [
    [getId("1"), "news (connect)", getAsset("ci_news.jpg"), 1, {}, "visible"],
    [getId("2"), "sports (connect)", getAsset("ci_sports.jpg"), 2, {}, "visible"],
    [getId("3"), "technology (connect)", getAsset("ci_technology.jpg"), 3, {}, "visible"],
    [getId("4"), "art (connect)", getAsset("ci_art.jpg"), 4, {}, "visible"],
    [getId("5"), "basic (connect)", getAsset("ci_basic.jpg"), 5, {}, "visible"],
    [getId("6"), "nature (connect)", getAsset("ci_nature.jpg"), 6, {}, "visible"],
    [getId("7"), "workplace (connect)", getAsset("ci_workplace.jpg"), 7, {}, "visible"],
    [getId("8"), "workplace (connect)", getAsset("ci_workplace.jpg"), 8, {}, "visible"],
    [getId("99"), "test hidden (connect)", getAsset("ci_workplace.jpg"), 9, {}, "hidden"],
  ];
  dirtyTruncateTable("QuizMFCategories");

  let lt_collection = $app.findCollectionByNameOrId("QuizMFCategories");

  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", lesson_type[2]);
    record.set("pos", lesson_type[3]);
    record.set("init_answer", lesson_type[4]);
    record.set("visible", lesson_type[5]);

    $app.save(record);
  }

  console.log(`030_QuizMFCategories 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();
};
