package main

import (
	"log"
	"os"

	"igit.com/xbase/raft"
	"igit.com/xbase/raft/example/basic/common"
)

func main() {
	// Configuration
	nodeID := "node2"
	addr := "127.0.0.1:9002"
	dataDir := "../data/node2"

	// Initial Cluster configuration (Node 1 + Node 2)
	clusterNodes := map[string]string{
		"node1": "127.0.0.1:9001",
		"node2": "127.0.0.1:9002",
	}

	config := raft.DefaultConfig()
	config.NodeID = nodeID
	config.ListenAddr = addr
	config.DataDir = dataDir
	config.ClusterNodes = clusterNodes
	config.LogCompactionEnabled = false
	
	config.Logger = raft.NewConsoleLogger(nodeID, 1)

	if err := os.MkdirAll(dataDir, 0755); err != nil {
		log.Fatalf("Failed to create data directory: %v", err)
	}

	server, err := raft.NewKVServer(config)
	if err != nil {
		log.Fatalf("Failed to create server: %v", err)
	}

	// Register demo commands
	common.RegisterDemoCommands(server.CLI)

	if err := server.Start(); err != nil {
		log.Fatalf("Failed to start server: %v", err)
	}
	defer server.Stop()

	// Keep main thread running
	select {}
}

