package test

import (
	"context"

	"github.com/aws/aws-sdk-go-v2/aws"
	awskmsv1alpha1 "github.com/crossplane/provider-aws/apis/kms/v1alpha1"
	"k8s.io/apimachinery/pkg/runtime"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"
)

// MockKMSKeyReconciler reconciles tidbcluster object in test env
type MockKMSkeyReconciler struct {
	client.Client
	Scheme *runtime.Scheme
}

func NewMockKMSKeyReconciler(mgr ctrl.Manager) *MockKMSkeyReconciler {
	return &MockKMSkeyReconciler{
		Client: mgr.GetClient(),
		Scheme: mgr.GetScheme(),
	}
}

func (r *MockKMSkeyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	key := &awskmsv1alpha1.Key{}
	err := r.Get(ctx, req.NamespacedName, key)
	if err != nil {
		return ctrl.Result{}, client.IgnoreNotFound(err)
	}
	key.Status.AtProvider.KeyID = aws.String("test")
	return ctrl.Result{}, r.Client.Status().Update(ctx, key)
}

// SetupWithManager sets up the controller with the Manager.
func (r *MockKMSkeyReconciler) SetupWithManager(mgr ctrl.Manager) error {
	return ctrl.NewControllerManagedBy(mgr).
		For(&awskmsv1alpha1.Key{}).
		Complete(r)
}
