import fs from "node:fs"
import path from "node:path"
import { argv, cwd, exit } from "node:process"

const getChainIdFromArgs = (): string | null => {
  const args = argv
  const chainIdArg = args.find((arg) => arg.startsWith("--chainId="))
  if (chainIdArg) {
    return chainIdArg.split("=")[1]
  }
  return null
}

const newChainIdString = getChainIdFromArgs()

if (!newChainIdString) {
  console.error(
    "Error: --chainId argument is missing. Please provide it like --chainId=12345"
  )
  exit(1)
}

const newChainIdNum = Number.parseInt(newChainIdString!, 10)
if (Number.isNaN(newChainIdNum)) {
  console.error("Error: --chainId must be a number.")
  exit(1)
}

const sourceFilePath = path.resolve(cwd(), "chains-testnet/84532.json")
const targetDirectory = path.resolve(cwd(), "chains-testnet")
const targetFilePath = path.join(targetDirectory, `${newChainIdString}.json`)

async function main() {
  try {
    const sourceContent = await fs.promises.readFile(sourceFilePath, "utf-8")
    const chainConfig = JSON.parse(sourceContent)

    const originalRpcFromTemplate = chainConfig.rpc

    chainConfig.name = newChainIdString
    chainConfig.chainId = newChainIdString

    const placeholderRpc = "<YOUR RPC URL (debug_traceCall enabled)>"
    if (chainConfig.rpc === placeholderRpc) {
      chainConfig.rpc = `http://host.docker.internal:${newChainIdString}`
    }

    const newJsonContent = JSON.stringify(chainConfig, null, 4)

    await fs.promises.writeFile(targetFilePath, newJsonContent)

    console.log(
      `Successfully duplicated and updated chain config to ${targetFilePath}`
    )
    if (originalRpcFromTemplate !== chainConfig.rpc) {
      console.log(
        `RPC in new file: ${chainConfig.rpc} (changed from: ${originalRpcFromTemplate})`
      )
    } else {
      console.log(
        `RPC in new file: ${chainConfig.rpc} (unchanged from template)`
      )
    }
  } catch (error) {
    if (error instanceof Error) {
      console.error(`An error occurred: ${error.message}`)
    } else {
      console.error("An unknown error occurred.")
    }
    exit(1)
  }
}

main()
