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

package defaults

import (
	"context"
	"encoding/json"
	"net/http"

	k8serrors "k8s.io/apimachinery/pkg/api/errors"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
	gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"

	capsulegateway "github.com/projectcapsule/capsule/internal/webhook/gateway"
	"github.com/projectcapsule/capsule/internal/webhook/utils"
	caperrors "github.com/projectcapsule/capsule/pkg/api/errors"
	ad "github.com/projectcapsule/capsule/pkg/runtime/admission"
)

func mutateGatewayDefaults(
	ctx context.Context,
	req admission.Request,
	c client.Client,
	decoder admission.Decoder,
	namespce string,
) *admission.Response {
	gatewayObj := &gatewayv1.Gateway{}
	if err := decoder.Decode(req, gatewayObj); err != nil {
		return ad.ErroredResponse(err)
	}

	gatewayObj.SetNamespace(namespce)

	tnt, err := capsulegateway.TenantFromGateway(ctx, c, gatewayObj)
	if err != nil {
		return ad.ErroredResponse(err)
	}

	if tnt == nil {
		return nil
	}

	allowed := tnt.Spec.GatewayOptions.AllowedClasses

	if allowed == nil || allowed.Default == "" {
		return nil
	}

	var mutate bool

	gatewayClass, err := utils.GetGatewayClassClassByObjectName(ctx, c, gatewayObj.Spec.GatewayClassName)

	if gatewayClass == nil {
		if gatewayObj.Spec.GatewayClassName == ("") {
			mutate = true
		} else {
			return ad.Deny(caperrors.NewGatewayError(gatewayObj.Spec.GatewayClassName, err).Error())
		}
	}

	if gatewayClass != nil && gatewayClass.Name != allowed.Default {
		if err != nil && !k8serrors.IsNotFound(err) {
			return ad.Deny(caperrors.NewGatewayClassError(gatewayClass.Name, err).Error())
		}
	} else {
		mutate = true
	}

	if mutate = mutate || (gatewayClass.Name == allowed.Default); !mutate {
		return nil
	}

	gatewayObj.Spec.GatewayClassName = gatewayv1.ObjectName(allowed.Default)

	marshaled, err := json.Marshal(gatewayObj)
	if err != nil {
		response := admission.Errored(http.StatusInternalServerError, err)

		return &response
	}

	response := admission.PatchResponseFromRaw(req.Object.Raw, marshaled)

	return &response
}
