/*
Copyright © The ESO 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

    https://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 generator

import (

	//nolint
	. "github.com/onsi/gomega"

	// nolint
	. "github.com/onsi/ginkgo/v2"
	v1 "k8s.io/api/core/v1"

	"github.com/external-secrets/external-secrets-e2e/framework"
	esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
	genv1alpha1 "github.com/external-secrets/external-secrets/apis/generators/v1alpha1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("fake generator", Label("fake"), func() {
	f := framework.New("fake")

	var (
		fakeGenData = map[string]string{
			"foo": "bar",
			"baz": "bang",
		}
	)

	injectGenerator := func(tc *testCase) {
		tc.Generator = &genv1alpha1.Fake{
			TypeMeta: metav1.TypeMeta{
				APIVersion: genv1alpha1.Group + "/" + genv1alpha1.Version,
				Kind:       genv1alpha1.FakeKind,
			},
			ObjectMeta: metav1.ObjectMeta{
				Name:      generatorName,
				Namespace: f.Namespace.Name,
			},
			Spec: genv1alpha1.FakeSpec{
				Data: fakeGenData,
			},
		}
	}

	customResourceGenerator := func(tc *testCase) {
		tc.ExternalSecret.Spec.DataFrom = []esv1.ExternalSecretDataFromRemoteRef{
			{
				SourceRef: &esv1.StoreGeneratorSourceRef{
					GeneratorRef: &esv1.GeneratorRef{
						// we don't need to specify the apiVersion,
						// this should be inferred by the controller.
						Kind: "Fake",
						Name: generatorName,
					},
				},
			},
		}
		tc.AfterSync = func(secret *v1.Secret) {
			for k, v := range fakeGenData {
				Expect(secret.Data[k]).To(Equal([]byte(v)))
			}
		}
	}

	DescribeTable("generate secrets with fake generator", generatorTableFunc,
		Entry("using custom resource generator", f, injectGenerator, customResourceGenerator),
	)
})
