// Copyright [2026] [Argus]
//
// 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.

//go:build unit

package deployedver

import (
	"testing"

	"github.com/release-argus/Argus/internal/test"
	"github.com/release-argus/Argus/service/deployed_version/types/base"
	"github.com/release-argus/Argus/service/deployed_version/types/web"
)

func TestIsEqual(t *testing.T) {
	dvCfg := plainDefaultsConfig(t)

	// GIVEN: two Lookups.
	tests := []struct {
		name string
		a, b Lookup
		want bool
	}{
		{
			name: "empty",
			a:    &web.Lookup{},
			b:    &web.Lookup{},
			want: true,
		},
		{
			name: "defaults ignored",
			a: &web.Lookup{
				Lookup: base.Lookup{
					Defaults: &base.Defaults{
						AllowInvalidCerts: new(false),
					},
				},
			},
			b:    &web.Lookup{},
			want: true,
		},
		{
			name: "hard_defaults ignored",
			a: &web.Lookup{
				Lookup: base.Lookup{
					Defaults: &base.Defaults{
						AllowInvalidCerts: new(false),
					},
				},
			},
			b:    &web.Lookup{},
			want: true,
		},
		{
			name: "equal",
			a: test.Must(t, func() (Lookup, error) {
				return Decode(
					"yaml", []byte(test.TrimYAML(`
						type: url
						method: GET
						url: https://example.com
						allow_invalid_certs: false
						basic_auth:
							username: user
							password: pass
						headers:
							- key: X-Header
								value: val
							- key: X-Another
								value: val2
						body: body_here
						json: value.version
						regex: v([0-9.]+)
						regex_template: $1
					`)),
					nil,
					nil,
					dvCfg,
				)
			}),
			b: test.Must(t, func() (Lookup, error) {
				return Decode(
					"yaml", []byte(test.TrimYAML(`
						type: url
						method: GET
						url: https://example.com
						allow_invalid_certs: false
						basic_auth:
							username: user
							password: pass
						headers:
							- key: X-Header
								value: val
							- key: X-Another
								value: val2
						body: body_here
						json: value.version
						regex: v([0-9.]+)
						regex_template: $1
					`)),
					nil,
					nil,
					dvCfg,
				)
			}),
			want: true,
		},
		{
			name: "not equal",
			a: test.Must(t, func() (Lookup, error) {
				return Decode(
					"yaml", []byte(test.TrimYAML(`
						type: url
						method: GET
						url: https://example.com
						allow_invalid_certs: false
						basic_auth:
							username: user
							password: pass
						headers:
							- key: X-Header
								value: val
							- key: X-Another
								value: val2
						body: body_here
						json: value.version
						regex: v([0-9.]+)
						regex_template: $1
					`)),
					nil,
					nil,
					dvCfg,
				)
			}),
			b: test.Must(t, func() (Lookup, error) {
				return Decode(
					"yaml", []byte(test.TrimYAML(`
						type: url
						method: GET
						url: https://example.com
						allow_invalid_certs: false
						basic_auth:
							username: user
							password: pass
						headers:
							- key: X-Header
								value: val
							- key: X-Another
								value: val3
						body: body_here
						json: value.version
						regex: v([0-9.]+)
						regex_template: $1
					`)),
					nil,
					nil,
					dvCfg,
				)
			}),
			want: false,
		},
		{
			name: "not equal with nil",
			a:    nil,
			b: &web.Lookup{
				URL: "https://example.com",
			},
			want: false,
		},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			// WHEN: the two Lookups are compared.
			got := IsEqual(tc.a, tc.b)

			// THEN: the result is as expected.
			if got != tc.want {
				t.Errorf(
					"%sIsEqual(a=%v, b=%v) result mismatch\ngot:  %t\nwant: %t",
					packageName, tc.a, tc.b,
					got, tc.want,
				)
			}
		})
	}
}
