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

package e2e

import (
	"context"

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

	capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
	"github.com/projectcapsule/capsule/pkg/api/meta"
	"github.com/projectcapsule/capsule/pkg/api/rbac"
)

var _ = Describe("creating a Namespace for a Tenant with required metadata", Ordered, Label("namespace", "metadata", "forbidden"), func() {
	tnt := &capsulev1beta2.Tenant{
		ObjectMeta: metav1.ObjectMeta{
			Name: "e2e-metadata-required",
			Labels: map[string]string{
				"env": "e2e",
			},
		},
		Spec: capsulev1beta2.TenantSpec{
			Owners: rbac.OwnerListSpec{
				{
					CoreOwnerSpec: rbac.CoreOwnerSpec{
						UserSpec: rbac.UserSpec{
							Name: "e2e-metadata-required",
							Kind: "User",
						},
					},
				},
			},
			NamespaceOptions: &capsulev1beta2.NamespaceOptions{
				RequiredMetadata: &capsulev1beta2.RequiredMetadata{
					Labels: map[string]string{
						"environment": "^(prod|test|dev)$",
					},
					Annotations: map[string]string{
						"example.corp/cost-center": "^INV-[0-9]{4}$",
					},
				},
			},
		},
	}

	JustBeforeEach(func() {
		EventuallyCreation(func() error {
			return k8sClient.Create(context.TODO(), tnt)
		}).Should(Succeed())
		TenantReady(tnt, metav1.ConditionTrue, defaultTimeoutInterval)
	})
	JustAfterEach(func() {
		EventuallyDeletion(tnt)
	})

	It("should contain required Namespace metadata", func() {
		By("creating without required label", func() {
			ns := NewNamespace("", map[string]string{
				meta.TenantLabel: tnt.GetName(),
			})

			NamespaceCreation(ns, tnt.Spec.Owners[0].UserSpec, defaultTimeoutInterval).ShouldNot(Succeed())
			NamespaceIsNotPartOfTenant(tnt, ns).Should(Succeed())

		})

		By("creating with required label, without annotation", func() {
			ns := NewNamespace("", map[string]string{
				"environment":    "prod",
				meta.TenantLabel: tnt.GetName(),
			})
			ns.SetAnnotations(map[string]string{})

			NamespaceCreation(ns, tnt.Spec.Owners[0].UserSpec, defaultTimeoutInterval).ShouldNot(Succeed())
			NamespaceIsNotPartOfTenant(tnt, ns).Should(Succeed())

		})

		By("creating with required label and annotation", func() {
			ns := NewNamespace("", map[string]string{
				"environment":    "prod",
				meta.TenantLabel: tnt.GetName(),
			})
			ns.SetAnnotations(map[string]string{
				"example.corp/cost-center": "INV-1234",
			})

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

			Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: ns.GetName()}, ns)).Should(Succeed())
			ns.SetLabels(map[string]string{
				"environment": "UAT",
			})

			ns.SetAnnotations(map[string]string{
				"example.corp/cost-center": "INV-1",
			})

			c := impersonationClient(tnt.Spec.Owners[0].UserSpec.Name, withDefaultGroups(nil))

			err := c.Update(context.TODO(), ns)
			Expect(err).ShouldNot(Succeed(), "expected failure")

		})

		By("creating with required label (wrong value) and annotation", func() {
			ns := NewNamespace("", map[string]string{
				"environment":    "UAT",
				meta.TenantLabel: tnt.GetName(),
			})
			ns.SetAnnotations(map[string]string{
				"example.corp/cost-center": "INV-1234",
			})

			NamespaceCreation(ns, tnt.Spec.Owners[0].UserSpec, defaultTimeoutInterval).ShouldNot(Succeed())
			NamespaceIsNotPartOfTenant(tnt, ns).Should(Succeed())
		})

		By("creating with required label and annotation (wrong value)", func() {
			ns := NewNamespace("", map[string]string{
				"environment":    "prod",
				meta.TenantLabel: tnt.GetName(),
			})
			ns.SetAnnotations(map[string]string{
				"example.corp/cost-center": "INV-1",
			})

			NamespaceCreation(ns, tnt.Spec.Owners[0].UserSpec, defaultTimeoutInterval).ShouldNot(Succeed())
			NamespaceIsNotPartOfTenant(tnt, ns).Should(Succeed())
		})
	})
})
