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 = [
    ["11".padStart(15, 0), "teacher1@123.com", "teacher1@123.com", "teacher1@123.com", true, true, "test_user_1"],
    ["12".padStart(15, 0), "teacher2@123.com", "teacher2@123.com", "teacher2@123.com", true, true, "test_user_2"],
    ["13".padStart(15, 0), "teacher3@123.com", "teacher3@123.com", "teacher3@123.com", true, true, "test_user_3"],
  ];

  let um_row_array = [
    [
      "11".padStart(15, 0),
      "teacher1@123.com",
      "active",
      "11".padStart(15, 0),
      JSON.stringify({}),
      getAsset("people1.png"),
      "teacher",
      //
    ],
    [
      "12".padStart(15, 0),
      "teacher2@123.com",
      "active",
      "12".padStart(15, 0),
      JSON.stringify({}),
      getAsset("people2.png"),
      "teacher",
      //
    ],
    [
      "13".padStart(15, 0),
      "teacher3@123.com",
      "active",
      "13".padStart(15, 0),
      JSON.stringify({}),
      getAsset("people3.png"),
      "teacher",
      //
    ],
  ];

  let users_collection = $app.findCollectionByNameOrId("users");
  let user_metas_collection = $app.findCollectionByNameOrId("UserMetas");

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

    let record = new Record(users_collection);
    record.set("id", user[0]);
    record.set("password", user[1]);
    record.set("passwordConfirm", user[2]);
    record.set("email", user[3]);
    record.set("emailVisibility", user[4]);
    record.set("verified", user[5]);
    record.set("name", user[6]);
    $app.save(record);

    let um_record = new Record(user_metas_collection);
    um_record.set("id", um[0]);
    um_record.set("helloworld", um[1]);
    um_record.set("state", um[2]);
    um_record.set("user_id", um[3]);
    um_record.set("meta", um[4]);
    um_record.set("avatar", um[5]);
    um_record.set("role", um[6]);
    $app.save(um_record);
  }

  console.log("005 add teacher user 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();
};
