/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package testutils

import (
	"net/netip"
	"reflect"
	"slices"
	"strings"
	"testing"

	log "github.com/sirupsen/logrus"

	"github.com/stretchr/testify/assert"

	"sigs.k8s.io/external-dns/endpoint"
	logtest "sigs.k8s.io/external-dns/internal/testutils/log"
)

func TestExampleSameEndpoints(t *testing.T) {
	eps := []*endpoint.Endpoint{
		{
			DNSName: "example.org",
			Targets: endpoint.Targets{"load-balancer.org"},
		},
		{
			DNSName:    "example.org",
			Targets:    endpoint.Targets{"load-balancer.org"},
			RecordType: endpoint.RecordTypeTXT,
		},
		{
			DNSName:    "abc.com",
			Targets:    endpoint.Targets{"something"},
			RecordType: endpoint.RecordTypeTXT,
		},
		{
			DNSName:       "abc.com",
			Targets:       endpoint.Targets{"1.2.3.4"},
			RecordType:    endpoint.RecordTypeA,
			SetIdentifier: "test-set-1",
		},
		{
			DNSName:    "bbc.com",
			Targets:    endpoint.Targets{"foo.com"},
			RecordType: endpoint.RecordTypeCNAME,
		},
		{
			DNSName:    "cbc.com",
			Targets:    endpoint.Targets{"foo.com"},
			RecordType: "CNAME",
			RecordTTL:  endpoint.TTL(60),
		},
		{
			DNSName: "example.org",
			Targets: endpoint.Targets{"load-balancer.org"},
			ProviderSpecific: endpoint.ProviderSpecific{
				endpoint.ProviderSpecificProperty{Name: "foo", Value: "bar"},
			},
		},
	}

	slices.SortFunc(eps, compareEndpoints)

	expectedOrder := []string{
		"abc.com",
		"abc.com",
		"bbc.com",
		"cbc.com",
		"example.org",
		"example.org",
		"example.org",
	}

	assert.Len(t, eps, len(expectedOrder))
	for i, ep := range eps {
		assert.Equal(t, expectedOrder[i], ep.DNSName, "endpoint %d should be %s", i, expectedOrder[i])
	}
}

func makeEndpoint(DNSName string) *endpoint.Endpoint { // nolint: gocritic // captLocal
	return &endpoint.Endpoint{
		DNSName:       DNSName,
		Targets:       endpoint.Targets{"target.com"},
		RecordType:    "A",
		SetIdentifier: "set1",
		RecordTTL:     300,
		Labels: map[string]string{
			endpoint.OwnerLabelKey:       "owner",
			endpoint.ResourceLabelKey:    "resource",
			endpoint.OwnedRecordLabelKey: "owned",
		},
		ProviderSpecific: endpoint.ProviderSpecific{
			{Name: "key", Value: "val"},
		},
	}
}

func TestSameEndpoint(t *testing.T) {
	tests := []struct {
		name           string
		a              *endpoint.Endpoint
		b              *endpoint.Endpoint
		isSameEndpoint bool
	}{
		{
			name:           "DNSName is not equal",
			a:              &endpoint.Endpoint{DNSName: "example.org"},
			b:              &endpoint.Endpoint{DNSName: "example.com"},
			isSameEndpoint: false,
		},
		{
			name: "All fields are equal",
			a: &endpoint.Endpoint{
				DNSName:       "example.org",
				Targets:       endpoint.Targets{"lb.example.com"},
				RecordType:    "A",
				SetIdentifier: "set-1",
				RecordTTL:     300,
				Labels: map[string]string{
					endpoint.OwnerLabelKey:       "owner-1",
					endpoint.ResourceLabelKey:    "resource-1",
					endpoint.OwnedRecordLabelKey: "owned-true",
				},
				ProviderSpecific: endpoint.ProviderSpecific{
					{Name: "key1", Value: "val1"},
				},
			},
			b: &endpoint.Endpoint{
				DNSName:       "example.org",
				Targets:       endpoint.Targets{"lb.example.com"},
				RecordType:    "A",
				SetIdentifier: "set-1",
				RecordTTL:     300,
				Labels: map[string]string{
					endpoint.OwnerLabelKey:       "owner-1",
					endpoint.ResourceLabelKey:    "resource-1",
					endpoint.OwnedRecordLabelKey: "owned-true",
				},
				ProviderSpecific: endpoint.ProviderSpecific{
					{Name: "key1", Value: "val1"},
				},
			},
			isSameEndpoint: true,
		},
		{
			name:           "Different Targets",
			a:              &endpoint.Endpoint{DNSName: "example.org", Targets: endpoint.Targets{"a.com"}},
			b:              &endpoint.Endpoint{DNSName: "example.org", Targets: endpoint.Targets{"b.com"}},
			isSameEndpoint: false,
		},
		{
			name:           "Different RecordType",
			a:              &endpoint.Endpoint{DNSName: "example.org", RecordType: "A"},
			b:              &endpoint.Endpoint{DNSName: "example.org", RecordType: "CNAME"},
			isSameEndpoint: false,
		},
		{
			name:           "Different SetIdentifier",
			a:              &endpoint.Endpoint{DNSName: "example.org", SetIdentifier: "id1"},
			b:              &endpoint.Endpoint{DNSName: "example.org", SetIdentifier: "id2"},
			isSameEndpoint: false,
		},
		{
			name: "Different OwnerLabelKey",
			a: &endpoint.Endpoint{
				DNSName: "example.org",
				Labels: map[string]string{
					endpoint.OwnerLabelKey: "owner1",
				},
			},
			b: &endpoint.Endpoint{
				DNSName: "example.org",
				Labels: map[string]string{
					endpoint.OwnerLabelKey: "owner2",
				},
			},
			isSameEndpoint: false,
		},
		{
			name:           "Different RecordTTL",
			a:              &endpoint.Endpoint{DNSName: "example.org", RecordTTL: 300},
			b:              &endpoint.Endpoint{DNSName: "example.org", RecordTTL: 400},
			isSameEndpoint: false,
		},
		{
			name: "Different ProviderSpecific",
			a: &endpoint.Endpoint{
				DNSName: "example.org",
				ProviderSpecific: endpoint.ProviderSpecific{
					{Name: "key1", Value: "val1"},
				},
			},
			b: &endpoint.Endpoint{
				DNSName: "example.org",
				ProviderSpecific: endpoint.ProviderSpecific{
					{Name: "key1", Value: "val2"},
				},
			},
			isSameEndpoint: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			isSameEndpoint := SameEndpoint(tt.a, tt.b)
			assert.Equal(t, tt.isSameEndpoint, isSameEndpoint)
		})
	}
}
func TestSameEndpoints(t *testing.T) {
	tests := []struct {
		name string
		a, b []*endpoint.Endpoint
		want bool
	}{
		{
			name: "Both slices nil",
			a:    nil,
			b:    nil,
			want: true,
		},
		{
			name: "One nil, one empty",
			a:    []*endpoint.Endpoint{},
			b:    nil,
			want: true,
		},
		{
			name: "Different lengths",
			a:    []*endpoint.Endpoint{makeEndpoint("a.com")},
			b:    []*endpoint.Endpoint{},
			want: false,
		},
		{
			name: "Same endpoints in same order",
			a:    []*endpoint.Endpoint{makeEndpoint("a.com"), makeEndpoint("b.com")},
			b:    []*endpoint.Endpoint{makeEndpoint("a.com"), makeEndpoint("b.com")},
			want: true,
		},
		{
			name: "Same endpoints in different order",
			a:    []*endpoint.Endpoint{makeEndpoint("b.com"), makeEndpoint("a.com")},
			b:    []*endpoint.Endpoint{makeEndpoint("a.com"), makeEndpoint("b.com")},
			want: true,
		},
		{
			name: "One endpoint differs",
			a:    []*endpoint.Endpoint{makeEndpoint("a.com"), makeEndpoint("b.com")},
			b:    []*endpoint.Endpoint{makeEndpoint("a.com"), makeEndpoint("c.com")},
			want: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			isSameEndpoints := SameEndpoints(tt.a, tt.b)
			assert.Equal(t, tt.want, isSameEndpoints)
		})
	}
}

func TestSameEndpointLabel(t *testing.T) {
	tests := []struct {
		name string
		a    []*endpoint.Endpoint
		b    []*endpoint.Endpoint
		want bool
	}{
		{
			name: "length of a and b are not same",
			a:    []*endpoint.Endpoint{makeEndpoint("a.com")},
			b:    []*endpoint.Endpoint{makeEndpoint("b.com"), makeEndpoint("c.com")},
			want: false,
		},
		{
			name: "endpoint's labels are same in a and b",
			a:    []*endpoint.Endpoint{makeEndpoint("a.com"), makeEndpoint("c.com")},
			b:    []*endpoint.Endpoint{makeEndpoint("b.com"), makeEndpoint("c.com")},
			want: true,
		},
		{
			name: "endpoint's labels are not same in a and b",
			a: []*endpoint.Endpoint{
				{
					DNSName: "a.com",
					Labels: endpoint.Labels{
						endpoint.OwnerLabelKey:    "owner1",
						endpoint.ResourceLabelKey: "resource1",
					},
				},
				{
					DNSName: "b.com",
					Labels: endpoint.Labels{
						endpoint.OwnerLabelKey:    "owner2",
						endpoint.ResourceLabelKey: "resource2",
					},
				},
			},
			b: []*endpoint.Endpoint{
				{
					DNSName: "a.com",
					Labels: endpoint.Labels{
						endpoint.OwnerLabelKey:    "owner",
						endpoint.ResourceLabelKey: "resource",
					},
				},
				{
					DNSName: "b.com",
					Labels: endpoint.Labels{
						endpoint.OwnerLabelKey:    "owner1",
						endpoint.ResourceLabelKey: "resource1",
					},
				},
			},
			want: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			isSameEndpointLabels := SameEndpointLabels(tt.a, tt.b)
			assert.Equal(t, tt.want, isSameEndpointLabels)
		})
	}
}

func TestSamePlanChanges(t *testing.T) {
	tests := []struct {
		name string
		a    map[string][]*endpoint.Endpoint
		b    map[string][]*endpoint.Endpoint
		want bool
	}{
		{
			name: "endpoints with all operations in a and b are same",
			a: map[string][]*endpoint.Endpoint{
				"Create":    {makeEndpoint("a.com")},
				"Delete":    {makeEndpoint("b.com")},
				"UpdateOld": {makeEndpoint("a.com")},
				"UpdateNew": {makeEndpoint("c.com")},
			},
			b: map[string][]*endpoint.Endpoint{
				"Create":    {makeEndpoint("a.com")},
				"Delete":    {makeEndpoint("b.com")},
				"UpdateOld": {makeEndpoint("a.com")},
				"UpdateNew": {makeEndpoint("c.com")},
			},
			want: true,
		},
		{
			name: "endpoints for create operations in a and b are not same",
			a: map[string][]*endpoint.Endpoint{
				"Create":    {makeEndpoint("a.com")},
				"Delete":    {makeEndpoint("b.com")},
				"UpdateOld": {makeEndpoint("a.com")},
				"UpdateNew": {makeEndpoint("c.com")},
			},
			b: map[string][]*endpoint.Endpoint{
				"Create":    {makeEndpoint("x.com")},
				"Delete":    {makeEndpoint("b.com")},
				"UpdateOld": {makeEndpoint("a.com")},
				"UpdateNew": {makeEndpoint("c.com")},
			},
			want: false,
		},
		{
			name: "endpoints for delete operations in a and b are not same",
			a: map[string][]*endpoint.Endpoint{
				"Create":    {makeEndpoint("a.com")},
				"Delete":    {makeEndpoint("b.com")},
				"UpdateOld": {makeEndpoint("a.com")},
				"UpdateNew": {makeEndpoint("c.com")},
			},
			b: map[string][]*endpoint.Endpoint{
				"Create":    {makeEndpoint("a.com")},
				"Delete":    {makeEndpoint("g.com")},
				"UpdateOld": {makeEndpoint("a.com")},
				"UpdateNew": {makeEndpoint("c.com")},
			},
			want: false,
		},
		{
			name: "endpoints for updateOld operations in a and b are not same",
			a: map[string][]*endpoint.Endpoint{
				"Create":    {makeEndpoint("a.com")},
				"Delete":    {makeEndpoint("b.com")},
				"UpdateOld": {makeEndpoint("b.com")},
				"UpdateNew": {makeEndpoint("c.com")},
			},
			b: map[string][]*endpoint.Endpoint{
				"Create":    {makeEndpoint("a.com")},
				"Delete":    {makeEndpoint("b.com")},
				"UpdateOld": {makeEndpoint("c.com")},
				"UpdateNew": {makeEndpoint("c.com")},
			},
			want: false,
		},
		{
			name: "endpoints for updateNew operations in a and b are same",
			a: map[string][]*endpoint.Endpoint{
				"Create":    {makeEndpoint("a.com")},
				"Delete":    {makeEndpoint("b.com")},
				"UpdateOld": {makeEndpoint("a.com")},
				"UpdateNew": {makeEndpoint("d.com")},
			},
			b: map[string][]*endpoint.Endpoint{
				"Create":    {makeEndpoint("a.com")},
				"Delete":    {makeEndpoint("b.com")},
				"UpdateOld": {makeEndpoint("a.com")},
				"UpdateNew": {makeEndpoint("c.com")},
			},
			want: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			checkPlanChanges := SamePlanChanges(tt.a, tt.b)
			assert.Equal(t, tt.want, checkPlanChanges)
		})
	}
}
func TestNewTargetsFromAddr(t *testing.T) {
	tests := []struct {
		name     string
		input    []netip.Addr
		expected endpoint.Targets
	}{
		{
			name:     "empty slice",
			input:    []netip.Addr{},
			expected: endpoint.Targets{},
		},
		{
			name: "single IPv4 address",
			input: []netip.Addr{
				netip.MustParseAddr("192.0.2.1"),
			},
			expected: endpoint.Targets{"192.0.2.1"},
		},
		{
			name: "multiple IP addresses",
			input: []netip.Addr{
				netip.MustParseAddr("192.0.2.1"),
				netip.MustParseAddr("2001:db8::1"),
			},
			expected: endpoint.Targets{"192.0.2.1", "2001:db8::1"},
		},
		{
			name: "IPv6 address only",
			input: []netip.Addr{
				netip.MustParseAddr("::1"),
			},
			expected: endpoint.Targets{"::1"},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := NewTargetsFromAddr(tt.input)
			if !reflect.DeepEqual(got, tt.expected) {
				t.Errorf("NewTargetsFromAddr() = %v, want %v", got, tt.expected)
			}
		})
	}
}

func TestWithLabel(t *testing.T) {
	e := &endpoint.Endpoint{}
	// should initialize Labels and set the key
	returned := e.WithLabel("foo", "bar")
	assert.Equal(t, e, returned)
	assert.NotNil(t, e.Labels)
	assert.Equal(t, "bar", e.Labels["foo"])

	// overriding an existing key
	e2 := e.WithLabel("foo", "baz")
	assert.Equal(t, e, e2)
	assert.Equal(t, "baz", e.Labels["foo"])

	// adding a new key without wiping others
	e.Labels["existing"] = "orig"
	e.WithLabel("new", "val")
	assert.Equal(t, "orig", e.Labels["existing"])
	assert.Equal(t, "val", e.Labels["new"])
}

func TestGenerateTestEndpointsWithDistribution(t *testing.T) {
	tests := []struct {
		name          string
		typeCounts    map[string]int
		domainWeights map[string]int
		ownerWeights  map[string]int
		wantTotal     int
		wantTypes     map[string]int
		wantDomains   map[string]int
		wantOwners    map[string]int
	}{
		{
			name:          "basic distribution",
			typeCounts:    map[string]int{"A": 6, "CNAME": 4},
			domainWeights: map[string]int{"example.com": 1, "test.org": 1},
			ownerWeights:  map[string]int{"owner1": 1, "owner2": 1},
			wantTotal:     10,
			wantTypes:     map[string]int{"A": 6, "CNAME": 4},
			wantDomains:   map[string]int{"example.com": 5, "test.org": 5},
			wantOwners:    map[string]int{"owner1": 5, "owner2": 5},
		},
		{
			name:          "weighted distribution 2:1",
			typeCounts:    map[string]int{"A": 9},
			domainWeights: map[string]int{"example.com": 2, "test.org": 1},
			ownerWeights:  map[string]int{"owner1": 2, "owner2": 1},
			wantTotal:     9,
			wantTypes:     map[string]int{"A": 9},
			wantDomains:   map[string]int{"example.com": 6, "test.org": 3},
			wantOwners:    map[string]int{"owner1": 6, "owner2": 3},
		},
		{
			name:          "empty weights use defaults",
			typeCounts:    map[string]int{"A": 3},
			domainWeights: map[string]int{},
			ownerWeights:  map[string]int{},
			wantTotal:     3,
			wantTypes:     map[string]int{"A": 3},
			wantDomains:   map[string]int{"example.com": 3},
			wantOwners:    map[string]int{},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			eps := GenerateTestEndpointsWithDistribution(tt.typeCounts, tt.domainWeights, tt.ownerWeights)

			assert.Len(t, eps, tt.wantTotal, "total endpoint count")

			// Count actual distributions
			gotTypes := make(map[string]int)
			gotDomains := make(map[string]int)
			gotOwners := make(map[string]int)

			for _, ep := range eps {
				gotTypes[ep.RecordType]++
				for domain := range tt.wantDomains {
					if strings.HasSuffix(ep.DNSName, domain) {
						gotDomains[domain]++
						break
					}
				}
				if owner, ok := ep.Labels[endpoint.OwnerLabelKey]; ok {
					gotOwners[owner]++
				}
			}

			assert.Equal(t, tt.wantTypes, gotTypes, "record type distribution")
			assert.Equal(t, tt.wantDomains, gotDomains, "domain distribution")
			assert.Equal(t, tt.wantOwners, gotOwners, "owner distribution")
		})
	}
}

func TestFilterEndpointsByOwnerIDLogging(t *testing.T) {
	noOwner := &endpoint.Endpoint{}
	ownedByFoo := &endpoint.Endpoint{
		Labels: endpoint.Labels{
			endpoint.OwnerLabelKey: "foo",
		},
	}
	ownedByBar := &endpoint.Endpoint{
		Labels: endpoint.Labels{
			endpoint.OwnerLabelKey: "bar",
		},
	}
	tests := []struct {
		name         string
		ownerID      string
		endpoints    []*endpoint.Endpoint
		messages     []string
		messages_not []string
		result       []*endpoint.Endpoint
	}{
		{
			name:         "one_matches",
			ownerID:      "foo",
			endpoints:    []*endpoint.Endpoint{ownedByFoo},
			messages:     []string{},
			messages_not: []string{""},
			result:       []*endpoint.Endpoint{ownedByFoo},
		},
		{
			name:         "wrong_owner",
			ownerID:      "foo",
			endpoints:    []*endpoint.Endpoint{ownedByFoo, ownedByBar},
			messages:     []string{"because owner id does not match"},
			messages_not: []string{},
			result:       []*endpoint.Endpoint{ownedByFoo},
		},
		{
			name:         "no_owner",
			ownerID:      "bar",
			endpoints:    []*endpoint.Endpoint{noOwner, ownedByBar},
			messages:     []string{"because of missing owner label"},
			messages_not: []string{"because owner id does not match"},
			result:       []*endpoint.Endpoint{ownedByBar},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			hook := logtest.LogsUnderTestWithLogLevel(log.DebugLevel, t)
			endpoint.FilterEndpointsByOwnerID(tt.ownerID, tt.endpoints)
			for _, m := range tt.messages {
				logtest.TestHelperLogContains(m, hook, t)
			}
			for _, m := range tt.messages_not {
				logtest.TestHelperLogNotContains(m, hook, t)
			}
		})
	}
}
