const XLSX = require("xlsx");
const fs = require("fs");
const path = require("path");

const filePath = "3Status.xlsx"; //File path of excel

const workbook = XLSX.readFile(filePath);
const worksheet = workbook.Sheets["Sheet0"];
const data = XLSX.utils.sheet_to_json(worksheet);
const folderPath = "test"; //File path of git TCs
const filePaths = [];

function getFilePaths(directory) {
  const items = fs.readdirSync(directory);
  for (const item of items) {
    const itemPath = path.join(directory, item);
    if (fs.statSync(itemPath).isDirectory()) {
      getFilePaths(itemPath);
    } else {
      filePaths.push(itemPath);
    }
  }
}
getFilePaths(folderPath);
function newProc(filePaths, keyword) {
  let flag1 = false;
  for (const singlePath of filePaths) {
    const fileContent = fs.readFileSync(singlePath, "utf-8");
    //console.log("fileContent is ", fileContent)
    if (fileContent.includes(keyword)) {
      flag1 = true;
      //console.log(`Keyword '${keyword}' found in file: ${singlePath}`);
    }
  }
  return flag1;
}
let notAutomated = [];
for (const i of data) {
  let flag = false;
  let check = newProc(filePaths, i["TestRail Id"]);
  //console.log("check is ", check)
  if (check == false) {
    notAutomated.push(i["TestRail Id"]);
    //console.log(`${i["TestRail Id"]} is not automated`)
  }
}
console.log(`notAutomated TCs are `, notAutomated);
console.log("No.of notAutomated are ", notAutomated.length);

//Storing the notAutomated ones in the excel file
const values = ["NonAutomatedTestRailIds", ...notAutomated];
const workbook1 = XLSX.utils.book_new();
const worksheet1 = XLSX.utils.aoa_to_sheet([]);
values.forEach((value, index) => {
  const cellAddress = XLSX.utils.encode_cell({ c: 0, r: index }); // column 0, row index
  worksheet1[cellAddress] = { v: value };
});
worksheet1["!ref"] = XLSX.utils.encode_range({
  s: { c: 0, r: 0 },
  e: { c: 0, r: values.length - 1 },
});
XLSX.utils.book_append_sheet(workbook1, worksheet1, "Sheet1");
XLSX.writeFile(workbook1, "NonAutomatedTestRailIdsFile.xlsx");