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:9501" // Raft Port
	httpAddr := ":8501"      // HTTP API Port
	dataDir := "../data/node2"

	// Cluster configuration
	clusterNodes := map[string]string{
		"node1": "127.0.0.1:9500",
		"node2": "127.0.0.1:9501",
	}

	config := raft.DefaultConfig()
	config.NodeID = nodeID
	config.ListenAddr = addr
	config.DataDir = dataDir
	config.ClusterNodes = clusterNodes
	
	// Enable HTTP API for WebHooks
	config.HTTPAddr = httpAddr
	
	// Console Logger
	config.Logger = raft.NewConsoleLogger(nodeID, 3) // Error level only (Quiet mode)

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

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

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

	// Start server
	if err := server.Start(); err != nil {
		log.Fatalf("Failed to start server: %v", err)
	}
	
	log.Printf("Raft Node %s started at %s", nodeID, addr)
	log.Printf("HTTP API started at %s", httpAddr)

	defer server.Stop()

	// Keep main thread running
	select {}
}

