import { fileURLToPath } from "node:url";
import path from "node:path";
import { loadConfig } from "./config.js";
import log4js from "log4js";
import { createElectronWindow } from "./electron/window.js";
import { setupIPC } from "./electron/ipc.js";
import { LocalServer } from "./localserver/localserver.js";
import { SocketClient } from "./socket/socketclient.js";
import {
  createCliLogger,
  isHeadless,
  startWaiting,
  stopWaiting,
} from "./commandlineutils.js";
import { handleCommandLine } from "./commandline.js";

if (!process.env.NODE_ENV) {
  process.env.NODE_ENV = "production";
}

const config = loadConfig();

const logger = log4js.getLogger("App");
logger.level = config.logLevel;

const cli = createCliLogger(logger);

export async function init(options = {}) {
  const headless = options.headless ?? isHeadless;
  cli.blank();

  const commandLineResult = await handleCommandLine(cli, logger);
  if (commandLineResult.handled) {
    return commandLineResult.result;
  }

  const runningServer = commandLineResult.runningServer;

  if (runningServer != false) {
    cli.warn("✘ Farm Control Server is already running.");
    cli.blank();
    return runningServer;
  }

  if (!headless) {
    startWaiting("Creating electron window...", logger);
    try {
      await createElectronWindow().catch((err) => {
        cli.warn("Failed to create Electron window:", err);
      });
      await stopWaiting();
    } catch (err) {
      await stopWaiting();
      throw err;
    }

    setupIPC().catch((err) => {
      cli.warn("Failed to setup IPC:", err);
    });
  } else {
    cli.info("Running in headless mode. Skipping window creation.");
  }

  const socketClient = new SocketClient();
  global.socketClient = socketClient;

  const localServer = new LocalServer(socketClient);
  await localServer.start();

  socketClient.connect();

  process.on("SIGINT", async () => {
    startWaiting("Shutting down...", logger);
    cli.blank();
    socketClient.disconnect();
    localServer.stop();
    await stopWaiting();
    process.exit(0);
  });
}

const isMainModule =
  process.argv[1] &&
  fileURLToPath(import.meta.url) === path.resolve(process.argv[1]);

if (isMainModule && !process.env.FC_HEADLESS_ENTRY) {
  init().catch((err) => {
    logger.error(err.message);
    cli.blank();
    process.exit(1);
  });
}
