package types

import (
	"testing"
	"time"
)

func TestGenerateID(t *testing.T) {
	id1 := GenerateID()
	id2 := GenerateID()

	if id1 == "" {
		t.Error("Generated ID should not be empty")
	}

	if id2 == "" {
		t.Error("Generated ID should not be empty")
	}

	if id1 == id2 {
		t.Error("Generated IDs should be unique")
	}

	if len(id1) != 32 {
		t.Errorf("Generated ID should be 32 characters long, got %d", len(id1))
	}
}

func TestNode(t *testing.T) {
	node := &Node{
		ID:        "test-id",
		Name:      "test-node",
		PublicKey: "test-key",
		IP:        []byte{10, 0, 0, 2},
		LastSeen:  time.Now(),
		IsOnline:  true,
	}

	if node.ID != "test-id" {
		t.Errorf("Expected ID 'test-id', got '%s'", node.ID)
	}

	if node.Name != "test-node" {
		t.Errorf("Expected Name 'test-node', got '%s'", node.Name)
	}

	if !node.IsOnline {
		t.Error("Node should be online")
	}
}

func TestMessageTypes(t *testing.T) {
	types := []MessageType{
		MessageTypeRegister,
		MessageTypeUpdate,
		MessageTypePeers,
		MessageTypePing,
		MessageTypePong,
	}

	expected := []string{
		"register",
		"update",
		"peers",
		"ping",
		"pong",
	}

	for i, msgType := range types {
		if string(msgType) != expected[i] {
			t.Errorf("Expected message type '%s', got '%s'", expected[i], string(msgType))
		}
	}
}

func TestRegisterRequest(t *testing.T) {
	req := RegisterRequest{
		NodeName:  "test-node",
		PublicKey: "test-key",
		Endpoint:  "192.168.1.100:51820",
	}

	if req.NodeName != "test-node" {
		t.Errorf("Expected NodeName 'test-node', got '%s'", req.NodeName)
	}

	if req.PublicKey != "test-key" {
		t.Errorf("Expected PublicKey 'test-key', got '%s'", req.PublicKey)
	}

	if req.Endpoint != "192.168.1.100:51820" {
		t.Errorf("Expected Endpoint '192.168.1.100:51820', got '%s'", req.Endpoint)
	}
}

func TestRegisterResponse(t *testing.T) {
	peers := []Peer{
		{
			PublicKey:  "peer1-key",
			AllowedIPs: []string{"10.0.0.2/32"},
			Endpoint:   "192.168.1.101:51820",
			KeepAlive:  25,
		},
	}

	resp := RegisterResponse{
		NodeID:  "test-id",
		IP:      "10.0.0.2",
		Network: "10.0.0.0/24",
		Peers:   peers,
	}

	if resp.NodeID != "test-id" {
		t.Errorf("Expected NodeID 'test-id', got '%s'", resp.NodeID)
	}

	if resp.IP != "10.0.0.2" {
		t.Errorf("Expected IP '10.0.0.2', got '%s'", resp.IP)
	}

	if len(resp.Peers) != 1 {
		t.Errorf("Expected 1 peer, got %d", len(resp.Peers))
	}

	if resp.Peers[0].PublicKey != "peer1-key" {
		t.Errorf("Expected peer public key 'peer1-key', got '%s'", resp.Peers[0].PublicKey)
	}
}

func TestPeer(t *testing.T) {
	peer := Peer{
		PublicKey:  "test-key",
		AllowedIPs: []string{"10.0.0.2/32", "10.0.0.3/32"},
		Endpoint:   "192.168.1.100:51820",
		KeepAlive:  25,
	}

	if peer.PublicKey != "test-key" {
		t.Errorf("Expected PublicKey 'test-key', got '%s'", peer.PublicKey)
	}

	if len(peer.AllowedIPs) != 2 {
		t.Errorf("Expected 2 allowed IPs, got %d", len(peer.AllowedIPs))
	}

	if peer.Endpoint != "192.168.1.100:51820" {
		t.Errorf("Expected Endpoint '192.168.1.100:51820', got '%s'", peer.Endpoint)
	}

	if peer.KeepAlive != 25 {
		t.Errorf("Expected KeepAlive 25, got %d", peer.KeepAlive)
	}
} 