package extnet

import (
	"testing"
	"time"
)

var testExtnetList = ListExtNet{
	{
		BridgeNetworkName:  "br-ext-net-01",
		DefaultGatewayIPv4: "192.168.1.1",
		DefaultGatewayIPv6: "2001:db8::1",
		Description:        "test1",
		Hypervisors: []string{
			"hv-node-01",
		},
		ID:        "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
		VersionID: 1747141621952,
		SubnetV4:  "192.168.1.0/24",
		SubnetV6:  "2001:db8::/64",
		CreatedAt: time.Date(2025, 10, 21, 20, 34, 30, 641000000, time.UTC),
		UpdatedAt: time.Date(2025, 10, 21, 20, 34, 30, 641000000, time.UTC),
		VLANTag:   100,
	},
	{
		BridgeNetworkName:  "br-backup-net-02",
		DefaultGatewayIPv4: "10.0.1.1",
		DefaultGatewayIPv6: "2001:db8:1::1",
		Description:        "test2",
		Hypervisors: []string{
			"hv-node-02",
		},
		ID:        "b2c3d4e5-f6g7-8901-bcde-f23456789012",
		VersionID: 1747141621953,
		SubnetV4:  "10.0.1.0/24",
		SubnetV6:  "2001:db8:1::/64",
		CreatedAt: time.Date(2025, 10, 21, 20, 34, 30, 641000000, time.UTC),
		UpdatedAt: time.Date(2025, 10, 21, 20, 34, 30, 641000000, time.UTC),
		VLANTag:   200,
	},
	{
		BridgeNetworkName:    "br-test-net-03",
		DefaultGatewayIPv4:   "172.16.1.1",
		DefaultGatewayIPv6:   "2001:db8:2::1",
		Description:          "test3",
		ExternalNetworkPorts: []ExternalNetworkPort{},
		Hypervisors: []string{
			"hv-node-05",
			"hv-node-06",
		},
		ID:        "c3d4e5f6-g7h8-9012-cdef-345678901234",
		VersionID: 1747141621954,
		SubnetV4:  "172.16.1.0/24",
		SubnetV6:  "2001:db8:2::/64",
		CreatedAt: time.Date(2025, 10, 21, 20, 34, 30, 641000000, time.UTC),
		UpdatedAt: time.Date(2025, 10, 21, 20, 34, 30, 641000000, time.UTC),
		VLANTag:   300,
	},
}

func TestFilterByID(t *testing.T) {
	actual := testExtnetList.FilterByID("a1b2c3d4-e5f6-7890-abcd-ef1234567890").FindOne()

	if actual.ID != "a1b2c3d4-e5f6-7890-abcd-ef1234567890" {
		t.Fatal("actual:", actual.ID, "> expected: a1b2c3d4-e5f6-7890-abcd-ef1234567890")
	}
}

func TestFilterByName(t *testing.T) {
	actual := testExtnetList.FilterByName("br-ext-net-01").FindOne()

	if actual.BridgeNetworkName != "br-ext-net-01" {
		t.Fatal("actual:", actual.BridgeNetworkName, ">> expected: br-ext-net-01")
	}
}

func TestFilterByIPv4(t *testing.T) {
	actual := testExtnetList.FilterByIPv4("192.168.1.1").FindOne()

	if actual.DefaultGatewayIPv4 != "192.168.1.1" {
		t.Fatal("actual:", actual.DefaultGatewayIPv4, ">> expected: 192.168.1.1")
	}
}

func TestFilterByIPv6(t *testing.T) {
	actual := testExtnetList.FilterByIPv6("2001:db8:1::1").FindOne()

	if actual.DefaultGatewayIPv6 != "2001:db8:1::1" {
		t.Fatal("actual:", actual.DefaultGatewayIPv6, ">> expected: 2001:db8:1::1")
	}
}

func TestFilterFunc(t *testing.T) {
	actual := testExtnetList.FilterFunc(func(response ExternalNetworkResponse) bool {
		return response.BridgeNetworkName == "br-backup-net-02"
	})

	if len(actual) != 1 || actual[0].ID != "b2c3d4e5-f6g7-8901-bcde-f23456789012" {
		t.Fatal("Expected 1 extnet with name 'br-backup-net-02', found:", len(actual))
	}
}

func TestFindOneWithResults(t *testing.T) {
	result := testExtnetList.FilterByID("c3d4e5f6-g7h8-9012-cdef-345678901234").FindOne()
	if result.ID != "c3d4e5f6-g7h8-9012-cdef-345678901234" {
		t.Fatal("Expected c3d4e5f6-g7h8-9012-cdef-345678901234, got:", result.ID)
	}
}

func TestFindOneEmpty(t *testing.T) {
	emptyList := ListExtNet{}
	result := emptyList.FindOne()

	if result.ID != "" || result.BridgeNetworkName != "" {
		t.Fatal("Expected empty extNet, got:", result)
	}
}

func TestFilterByIDNotFound(t *testing.T) {
	actual := ListExtNet{}.FilterByID("nonexistent")

	if len(actual) != 0 {
		t.Fatal("Expected 0 extNet, found:", len(actual))
	}
}

func TestFilterByNameNotFound(t *testing.T) {
	actual := ListExtNet{}.FilterByName("Nonexistent")

	if len(actual) != 0 {
		t.Fatal("Expected 0 extNet, found:", len(actual))
	}
}

func TestFilterByIPv4NotFound(t *testing.T) {
	actual := testExtnetList.FilterByIPv4("nonexistent-ip")

	if len(actual) != 0 {
		t.Fatal("Expected 0 extNet, found:", len(actual))
	}
}

func TestFilterByIPv6NotFound(t *testing.T) {
	actual := testExtnetList.FilterByIPv6("nonexistent-ipv6")

	if len(actual) != 0 {
		t.Fatal("Expected 0 extNet, found:", len(actual))
	}
}
