import { cmd } from "./cmd"
import { bootstrap } from "../bootstrap"
import { SaeeolSessions } from "@/sessions/sessions"
import { Instance } from "@/project/instance"
import { InstanceStore } from "@/project/instance-store"

export const RemoteCommand = cmd({
  command: "remote",
  describe: "enable remote connection for real-time session relay",
  builder: (yargs) => yargs,
  handler: async () => {
    await bootstrap(process.cwd(), async () => {
      await SaeeolSessions.enableRemote()
      console.log("Remote connection enabled.")

      const abort = new AbortController()
      const shutdown = async () => {
        try {
          SaeeolSessions.disableRemote()
          await InstanceStore.disposeInstance(Instance.current)
        } finally {
          abort.abort()
        }
      }
      process.on("SIGTERM", shutdown)
      process.on("SIGINT", shutdown)
      process.on("SIGHUP", shutdown)
      await new Promise((resolve) => abort.signal.addEventListener("abort", resolve))
    })
  },
})
