// Copyright 2020-2023 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
	"context"

	"github.com/projectcapsule/capsule/pkg/api/meta"
	"github.com/projectcapsule/capsule/pkg/api/rbac"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/types"

	capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
)

var _ = Describe("creating a Namespace with Tenant selector when user owns multiple tenants", Ordered, Label("tenant", "assignment"), func() {
	t1 := &capsulev1beta2.Tenant{
		ObjectMeta: metav1.ObjectMeta{
			Name: "e2e-tenant-label-one",
			Labels: map[string]string{
				"env": "e2e",
			},
		},
		Spec: capsulev1beta2.TenantSpec{
			Owners: rbac.OwnerListSpec{
				{
					CoreOwnerSpec: rbac.CoreOwnerSpec{
						UserSpec: rbac.UserSpec{
							Name: "e2e-tenant-label",
							Kind: "User",
						},
					},
				},
			},
		},
	}
	t2 := &capsulev1beta2.Tenant{
		ObjectMeta: metav1.ObjectMeta{
			Name: "e2e-tenant-label-two",
			Labels: map[string]string{
				"env": "e2e",
			},
		},
		Spec: capsulev1beta2.TenantSpec{
			Owners: rbac.OwnerListSpec{
				{
					CoreOwnerSpec: rbac.CoreOwnerSpec{
						UserSpec: rbac.UserSpec{
							Name: "e2e-tenant-label",
							Kind: "User",
						},
					},
				},
			},
		},
	}

	JustBeforeEach(func() {
		EventuallyCreation(func() error {
			t1.ResourceVersion = ""
			return k8sClient.Create(context.TODO(), t1)
		}).Should(Succeed())
		TenantReady(t1, metav1.ConditionTrue, defaultTimeoutInterval)

		EventuallyCreation(func() error {
			t2.ResourceVersion = ""
			return k8sClient.Create(context.TODO(), t2)
		}).Should(Succeed())
		TenantReady(t2, metav1.ConditionTrue, defaultTimeoutInterval)
	})
	JustAfterEach(func() {
		EventuallyDeletion(t1)
		EventuallyDeletion(t2)
	})

	It("should be assigned to the selected Tenant", func() {
		ns := NewNamespace("", map[string]string{
			meta.TenantLabel: t2.Name,
		})

		NamespaceCreation(ns, t2.Spec.Owners[0].UserSpec, defaultTimeoutInterval).Should(Succeed())
		NamespaceIsPartOfTenant(t2, ns).Should(Succeed())
	})

	It("prevent reassignment via labels from owners", func() {
		ns := NewNamespace("")
		By("assigning to the Namespace the Capsule Tenant label", func() {
			ns.Labels = map[string]string{
				meta.TenantLabel: t1.Name,
			}

			NamespaceCreation(ns, t1.Spec.Owners[0].UserSpec, defaultTimeoutInterval).Should(Succeed())
			NamespaceIsPartOfTenant(t1, ns).Should(Succeed())
		})

		By("assigning to the Namespace the Capsule Tenant label (Attempt Label Patch)", func() {
			patch := map[string]interface{}{
				"metadata": map[string]interface{}{
					"labels": map[string]string{
						meta.TenantLabel: t2.Name,
					},
				},
			}

			err := PatchNamespace(ns, ownerClient(t2.Spec.Owners[0].UserSpec), patch)
			Expect(err).NotTo(HaveOccurred())

			new := &corev1.Namespace{}
			k8sClient.Get(context.TODO(), types.NamespacedName{Name: ns.GetName()}, new)

			NamespaceIsPartOfTenant(t1, new).Should(Succeed())
		})

		By("assigning to the Namespace the Capsule Tenant label (Attempt Ownerreference Patch)", func() {
			ref, err := GetTenantOwnerReferenceAsPatch(t2)
			Expect(err).NotTo(HaveOccurred())

			patch := map[string]interface{}{
				"metadata": map[string]interface{}{
					"labels": map[string]string{
						meta.TenantLabel: t1.Name,
					},
					"ownerReferences": []map[string]interface{}{ref},
				},
			}

			err = PatchNamespace(ns, ownerClient(t2.Spec.Owners[0].UserSpec), patch)
			Expect(err).NotTo(HaveOccurred())

			new := &corev1.Namespace{}
			k8sClient.Get(context.TODO(), types.NamespacedName{Name: ns.GetName()}, new)

			NamespaceIsPartOfTenant(t1, new).Should(Succeed())
		})

		By("assigning to the Namespace the Capsule Tenant label (Attempt Ownerreference Patch) - Without Label", func() {
			ref, err := GetTenantOwnerReferenceAsPatch(t2)
			Expect(err).NotTo(HaveOccurred())

			patch := map[string]interface{}{
				"metadata": map[string]interface{}{
					"labels": map[string]interface{}{
						meta.TenantLabel: nil,
					},
					"ownerReferences": []map[string]interface{}{ref},
				},
			}

			err = PatchNamespace(ns, ownerClient(t2.Spec.Owners[0].UserSpec), patch)
			Expect(err).To(HaveOccurred())

			new := &corev1.Namespace{}
			k8sClient.Get(context.TODO(), types.NamespacedName{Name: ns.GetName()}, new)

			NamespaceIsPartOfTenant(t1, new).Should(Succeed())
		})

		By("assigning to the Namespace the Capsule Tenant label (Empty Ownerreferences)", func() {
			patch := map[string]interface{}{
				"metadata": map[string]interface{}{
					"labels": map[string]string{
						meta.TenantLabel: t2.Name,
					},
					"ownerReferences": []string{},
				},
			}

			err := PatchNamespace(ns, ownerClient(t2.Spec.Owners[0].UserSpec), patch)
			Expect(err).To(HaveOccurred())

			new := &corev1.Namespace{}
			k8sClient.Get(context.TODO(), types.NamespacedName{Name: ns.GetName()}, new)

			NamespaceIsPartOfTenant(t1, new).Should(Succeed())
		})

		By("assigning to the Namespace the Capsule Tenant label (Empty Ownerreferences) - Without Label", func() {
			patch := map[string]interface{}{
				"metadata": map[string]interface{}{
					"labels": map[string]interface{}{
						meta.TenantLabel: nil,
					},
					"ownerReferences": []string{},
				},
			}

			err := PatchNamespace(ns, ownerClient(t2.Spec.Owners[0].UserSpec), patch)
			Expect(err).To(HaveOccurred())

			new := &corev1.Namespace{}
			k8sClient.Get(context.TODO(), types.NamespacedName{Name: ns.GetName()}, new)

			NamespaceIsPartOfTenant(t1, new).Should(Succeed())
		})

		By("assigning to the Namespace the Capsule Tenant label (2nd Tenant Label + Ownerreference)", func() {
			ref, err := GetTenantOwnerReferenceAsPatch(t2)
			Expect(err).NotTo(HaveOccurred())

			patch := map[string]interface{}{
				"metadata": map[string]interface{}{
					"labels": map[string]string{
						meta.TenantLabel: t2.Name,
					},
					"ownerReferences": []map[string]interface{}{ref},
				},
			}

			err = PatchNamespace(ns, ownerClient(t2.Spec.Owners[0].UserSpec), patch)
			Expect(err).To(HaveOccurred())

			new := &corev1.Namespace{}
			k8sClient.Get(context.TODO(), types.NamespacedName{Name: ns.GetName()}, new)

			NamespaceIsPartOfTenant(t1, ns).Should(Succeed())
		})
	})
})
