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

package generic

import (
	"context"

	admissionv1 "k8s.io/api/admission/v1"
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

	"github.com/projectcapsule/capsule/pkg/api/meta"
	ad "github.com/projectcapsule/capsule/pkg/runtime/admission"
	clt "github.com/projectcapsule/capsule/pkg/runtime/client"
	"github.com/projectcapsule/capsule/pkg/runtime/events"
	"github.com/projectcapsule/capsule/pkg/runtime/handlers"
	"github.com/projectcapsule/capsule/pkg/tenant"
)

type tenantAssignmentHandler struct{}

func TenantAssignmentHandler() handlers.Handler {
	return &tenantAssignmentHandler{}
}

func (r *tenantAssignmentHandler) OnCreate(
	_ client.Client,
	reader client.Reader,
	decoder admission.Decoder,
	_ events.EventRecorder,
) handlers.Func {
	return func(ctx context.Context, req admission.Request) *admission.Response {
		return r.handle(ctx, reader, decoder, req)
	}
}

func (r *tenantAssignmentHandler) OnDelete(
	client.Client,
	client.Reader,
	admission.Decoder,
	events.EventRecorder,
) handlers.Func {
	return func(context.Context, admission.Request) *admission.Response {
		return nil
	}
}

func (r *tenantAssignmentHandler) OnUpdate(
	_ client.Client,
	reader client.Reader,
	decoder admission.Decoder,
	_ events.EventRecorder,
) handlers.Func {
	return func(ctx context.Context, req admission.Request) *admission.Response {
		return r.handle(ctx, reader, decoder, req)
	}
}

func (r *tenantAssignmentHandler) handle(
	ctx context.Context,
	c client.Reader,
	decoder admission.Decoder,
	req admission.Request,
) *admission.Response {
	if req.Namespace == "" {
		return nil
	}

	obj := &metav1.PartialObjectMetadata{}
	if err := decoder.Decode(req, obj); err != nil {
		return ad.ErroredResponse(err)
	}

	tnt, err := tenant.GetTenantNameByNamespace(ctx, c, req.Namespace)
	if err != nil {
		if apierrors.IsNotFound(err) {
			return nil
		}

		return ad.ErroredResponse(err)
	}

	if tnt == "" {
		return nil
	}

	labels := obj.GetLabels()

	desired := map[string]string{}
	if labels == nil || labels[meta.ManagedByCapsuleLabel] != tnt {
		desired[meta.ManagedByCapsuleLabel] = tnt
	}

	if labels == nil || labels[meta.NewTenantLabel] != tnt {
		desired[meta.NewTenantLabel] = tnt
	}

	patches := clt.AddLabelsPatch(labels, desired)
	if len(patches) == 0 {
		return nil
	}

	return &admission.Response{
		AdmissionResponse: admissionv1.AdmissionResponse{
			Allowed: true,
		},
		Patches: clt.JSONPatchesToJSONPatchOperation(patches),
	}
}
