const socketIOClient = require("socket.io-client");
const os = require("os");
const shell = require("shelljs");
const { findYmlFiles } = require("./index.js");
const { Act } = require("./kie-act-js/build/src/index.js");
const { v4: uuidv4 } = require("uuid");
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');

const act = new Act();

function startClient() {
  // const socket = socketIOClient("https://services.onetab.ai/");
  // const socket = socketIOClient("http://localhost:3008/");
  // const socket = socketIOClient("https://stg-services.onetab.ai/");

  const socket = socketIOClient("https://stg-services.onetab.ai/", {
  query: {
    repoFullname: extractRepoFullNameFromYaml(),
  }
});

  async function executeDockerCommands() {
    try {
      const platform = os.platform();
      const dockerCommand =
        platform === "win32"
          ? "start Docker.exe"
          : platform === "darwin"
          ? "open -a Docker"
          : "docker";

      await new Promise((resolve, reject) => {
        shell.exec(dockerCommand, { async: true }, (code, stdout, stderr) => {
          if (code === 0) {
            console.log("Docker command executed successfully");
            datafetch().then(resolve).catch(reject);
          } else {
            console.error("Error executing Docker command:", stderr);
            reject(new Error(stderr));
          }
        });
      });
    } catch (error) {
      console.error("Error executing Docker commands:", error);
    }
  }

  socket.on("sendDataToPackage", (payload) => {
    const jsonPayload = JSON.stringify(payload);
    console.log(`Client: Received json payload from backend: ${jsonPayload}`);
    if (payload) {
      const ymlFiles = findYmlFiles(jsonPayload);
      if (ymlFiles?.length) {
        executeDockerCommands();
      }
      else {
        console.log(`Branch mismatch: Push event branch  does not match yml file branch .`);
      }
    }
  });

  const datafetch = async () => {
    const uniqueId = uuidv4();
    const data = await act.list();
    console.log("workflow list-->", data);
    await act.dryRun(undefined, undefined, undefined, uniqueId);
    let result = await act.runEvent("push", { verbose: true, uuid: uniqueId });
    // console.log('result --------------------------------',result)
    socket.emit("workflowResult", result);
  };
}

function extractRepoFullNameFromYaml(searchPath = process.cwd()) {
  try {
    let yamlFilePath = null;

    function traverseDir(currentDir) {
      const files = fs.readdirSync(currentDir);
      for (const file of files) {
        const filePath = path.join(currentDir, file);
        if (fs.statSync(filePath).isDirectory()) {
          if (path.basename(filePath) !== "node_modules") {
            traverseDir(filePath);
          }
        } else if (file.endsWith("onetab-pipeline.yml")) {
          yamlFilePath = filePath;
          break;  // Stop traversal once the file is found
        }
      }
    }

    traverseDir(searchPath);

    if (!yamlFilePath) {
      console.log('Failed to locate onetab-pipeline yml file.');
      return null;
    }

    const fileContent = fs.readFileSync(yamlFilePath, 'utf8');
    const yamlData = yaml.load(fileContent);
    const repoFullName = yamlData?.repoFullName;

    return repoFullName;
  } catch (error) {
    console.error(`Error extracting repository full name from onetab-pipeline.yml:`, error.message);
    return null;
  }
}

startClient();