package main

import (
	"log"
	"os"

	"igit.com/xbase/raft"
)

func main() {
	// 1. 配置初始化
	config := raft.DefaultConfig()
	config.NodeID = "node1"
	config.ListenAddr = "127.0.0.1:9001"
	config.HTTPAddr = ":8001" // Enable HTTP API
	config.DataDir = "data/node1"
	config.Logger = &raft.DefaultLogger{Prefix: "node1"}

	// 初始集群配置
	config.ClusterNodes = map[string]string{
		"node1": "127.0.0.1:9001",
	}

	// 2. 创建 KV Server
	// Ensure data directory exists
	os.MkdirAll(config.DataDir, 0755)
	
	server, err := raft.NewKVServer(config)
	if err != nil {
		log.Fatalf("Failed to create server: %v", err)
	}

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

	// Register Demo Commands
	RegisterDemoAuthCommands(server.CLI)

	log.Println("Node1 started on 127.0.0.1:9001 (HTTP :8001)")
	log.Println("CLI is ready. Type 'help' to see commands.")

	// Block forever
	select {}
}

