package k8s

import (
	"testing"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	"github.com/ocdp/cluster-service/internal/domain/entity"
)

func TestResourceMatchesInstance(t *testing.T) {
	instance := &entity.Instance{
		Name:      "demo",
		Namespace: "default",
	}

	testCases := []struct {
		name string
		meta metav1.ObjectMeta
		want bool
	}{
		{
			name: "matches by standard label",
			meta: metav1.ObjectMeta{Labels: map[string]string{
				"app.kubernetes.io/instance": "demo",
			}},
			want: true,
		},
		{
			name: "matches by helm annotations",
			meta: metav1.ObjectMeta{Annotations: map[string]string{
				"meta.helm.sh/release-name":      "demo",
				"meta.helm.sh/release-namespace": "default",
			}},
			want: true,
		},
		{
			name: "matches by resource name prefix",
			meta: metav1.ObjectMeta{Name: "demo-nginx"},
			want: true,
		},
		{
			name: "does not match unrelated resource",
			meta: metav1.ObjectMeta{Name: "other"},
			want: false,
		},
	}

	for _, tc := range testCases {
		if got := resourceMatchesInstance(tc.meta, instance); got != tc.want {
			t.Fatalf("%s: expected %v, got %v", tc.name, tc.want, got)
		}
	}
}
