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), "user1@123.com", "user1@123.com", "user1@123.com", true, true, "test_user_1"],
    ["2".padStart(15, 0), "user2@123.com", "user2@123.com", "user2@123.com", true, true, "test_user_2"],
    ["3".padStart(15, 0), "user3@123.com", "user3@123.com", "user3@123.com", true, true, "test_user_3"],
    ["4".padStart(15, 0), "user4@123.com", "user4@123.com", "user4@123.com", true, true, "test_user_4"],
    ["5".padStart(15, 0), "user5@123.com", "user5@123.com", "user5@123.com", true, true, "test_user_5"],
  ];

  let um_row_array = [
    ["1".padStart(15, 0), "user1@123.com", "active", "1".padStart(15, 0), JSON.stringify({}), getAsset("people1.png"), "student"],
    ["2".padStart(15, 0), "user2@123.com", "active", "2".padStart(15, 0), JSON.stringify({}), getAsset("people2.png"), "student"],
    ["3".padStart(15, 0), "user3@123.com", "active", "3".padStart(15, 0), JSON.stringify({}), getAsset("people3.png"), "student"],
    ["4".padStart(15, 0), "user4@123.com", "active", "4".padStart(15, 0), JSON.stringify({}), getAsset("people4.png"), "student"],
    ["5".padStart(15, 0), "user5@123.com", "active", "5".padStart(15, 0), JSON.stringify({}), getAsset("people5.png"), "student"],
  ];

  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("006 add student 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();
};
