# Standalone cluster-config.json generator
# Usage: nix-build cluster-config-generator.nix --argstr hostname node01 --argstr clusterName plasmacloud
{ pkgs ? import <nixpkgs> {}
, hostname ? "node01"
, clusterName ? "plasmacloud"
, topologyFile ? ./example-topology.nix
}:

let
  # Import topology module
  lib = pkgs.lib;
  clusterConfigLib = import ../cluster-config-lib.nix { inherit lib; };

  # Evaluate the topology file
  topologyEval = import topologyFile { inherit lib; };

  # Get the cluster configuration
  cluster = topologyEval.nix-nos.clusters.${clusterName} or (throw "Cluster ${clusterName} not found");

  # Generate cluster config
  clusterConfig = clusterConfigLib.mkClusterConfig {
    inherit cluster hostname;
    bootstrapNodeName =
      if cluster ? bootstrapNode && cluster.bootstrapNode != null
      then cluster.bootstrapNode
      else null;
  };

  # Convert to JSON
  configJson = builtins.toJSON clusterConfig;

in pkgs.writeTextFile {
  name = "cluster-config-${hostname}.json";
  text = configJson;
  destination = "/cluster-config.json";
}
