package segments

import (
	"testing"
	"time"
)

var testSegmentList = ListSegment{
	{
		AccessGroupID:    "a1b2c3d4-1234-5678-9101-abcdef123456",
		AccessGroupName:  "default-access-group",
		CreatedAt:        time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC),
		Description:      "Test1",
		DHCPv4:           DHCPv4Config{},
		DHCPv6:           DHCPv6Config{},
		DisplayName:      "Test1",
		Enabled:          true,
		ID:               "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
		LogicalPortsInfo: []EntityInfo{},
		RoutersInfo:      []EntityInfo{},
		Status:           Status{},
		SubnetV4:         "192.168.1.0/24",
		SubnetV6:         "2001:db8::/64",
		UpdatedAt:        time.Date(2024, 1, 20, 14, 45, 0, 0, time.UTC),
		VersionID:        1,
	},
	{
		AccessGroupID:    "b2c3d4e5-2345-6789-0123-bcdefa654321",
		AccessGroupName:  "restricted-group",
		CreatedAt:        time.Date(2024, 2, 1, 9, 0, 0, 0, time.UTC),
		Description:      "Test2",
		DHCPv4:           DHCPv4Config{},
		DHCPv6:           DHCPv6Config{},
		DisplayName:      "Test2",
		Enabled:          false,
		ID:               "c3d4e5f6-3456-7890-1234-cdefab098765",
		LogicalPortsInfo: []EntityInfo{},
		RoutersInfo:      []EntityInfo{},
		Status:           Status{},
		SubnetV4:         "10.0.0.0/24",
		SubnetV6:         "2001:db8:1::/64",
		UpdatedAt:        time.Date(2024, 2, 5, 16, 20, 0, 0, time.UTC),
		VersionID:        2,
	},
	{
		AccessGroupID:    "d4e5f6a7-4567-8901-2345-defabc123456",
		AccessGroupName:  "minimal-group",
		CreatedAt:        time.Date(2024, 3, 10, 12, 0, 0, 0, time.UTC),
		Description:      "Test3",
		DHCPv4:           DHCPv4Config{},
		DHCPv6:           DHCPv6Config{},
		DisplayName:      "Test3",
		Enabled:          true,
		ID:               "e5f6a7b8-5678-9012-3456-efabcd789012",
		LogicalPortsInfo: []EntityInfo{},
		RoutersInfo:      []EntityInfo{},
		Status:           Status{},
		SubnetV4:         "192.15.1.0/24",
		SubnetV6:         "",
		UpdatedAt:        time.Date(2024, 3, 10, 12, 0, 0, 0, time.UTC),
		VersionID:        1,
	},
}

func TestFilterByID(t *testing.T) {
	actual := testSegmentList.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 := testSegmentList.FilterByName("Test1").FindOne()

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

func TestFilterBySubnetIPv4(t *testing.T) {
	actual := testSegmentList.FilterBySubnetIPv4("192.168.1.0/24").FindOne()

	if actual.SubnetV4 != "192.168.1.0/24" {
		t.Fatal("actual:", actual.SubnetV4, ">> expected: 192.168.1.0/24")
	}
}

func TestFilterBySubnetIPv6(t *testing.T) {
	actual := testSegmentList.FilterBySubnetIPv6("2001:db8::/64").FindOne()

	if actual.SubnetV6 != "2001:db8::/64" {
		t.Fatal("actual:", actual.SubnetV6, ">> expected: 2001:db8::/64")
	}
}

func TestFilterFunc(t *testing.T) {
	actual := testSegmentList.FilterFunc(func(response SegmentResponse) bool {
		return response.DisplayName == "Test2"
	})

	if len(actual) != 1 || actual[0].ID != "c3d4e5f6-3456-7890-1234-cdefab098765" {
		t.Fatal("Expected 1 extnet with name 'Test2', found:", len(actual))
	}
}

func TestFindOneWithResults(t *testing.T) {
	result := testSegmentList.FilterByID("c3d4e5f6-3456-7890-1234-cdefab098765").FindOne()
	if result.ID != "c3d4e5f6-3456-7890-1234-cdefab098765" {
		t.Fatal("Expected c3d4e5f6-3456-7890-1234-cdefab098765, got:", result.ID)
	}
}

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

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

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

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

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

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

func TestFilterBySegmentIPv4NotFound(t *testing.T) {
	actual := testSegmentList.FilterBySubnetIPv4("nonexistent-ip")

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

func TestFilterBySubnetIPv6NotFound(t *testing.T) {
	actual := testSegmentList.FilterBySubnetIPv6("nonexistent-ipv6")

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