const db = require("./dbConfig");

const generateID = async (user, branch_id) => {
  const date = new Date().toISOString().slice(2, 8).replace(/-/g, "");
  const sql = "SELECT code_name FROM hospitals WHERE branch_id=?";
  const [facility] = await db.execute(sql, [branch_id]);
  if (!facility)
    throw "facility code_name not found... please contact the developer if this persist";
  // console(facility);
  const hFacility = facility[0]["code_name"];

  if (user.cadre) {
    const shortCadre = user.cadre.slice(0, 3).toUpperCase();
    const id = user.id + "/" + shortCadre + "/" + date;
    return id;
  } else {
    const id = user.id + "/" + date + "/" + hFacility;
    return id;
  }
};

const generateSessionId = () => {
  return `${Math.random().toString(36).slice(2, 7)}.${Date.now()}`;
};

module.exports = { generateID, generateSessionId };
