package release

import (
	"net/http"

	"github.com/porter-dev/porter/api/server/handlers"
	"github.com/porter-dev/porter/api/server/shared"
	"github.com/porter-dev/porter/api/server/shared/apierrors"
	"github.com/porter-dev/porter/api/server/shared/config"
	"github.com/porter-dev/porter/api/server/shared/requestutils"
	"github.com/porter-dev/porter/api/types"
	"github.com/porter-dev/porter/internal/models"
	"gorm.io/gorm"
)

type GetNotificationHandler struct {
	handlers.PorterHandlerReadWriter
}

func NewGetNotificationHandler(
	config *config.Config,
	writer shared.ResultWriter,
) *GetNotificationHandler {
	return &GetNotificationHandler{
		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
	}
}

func (c *GetNotificationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
	name, _ := requestutils.GetURLParamString(r, types.URLParamReleaseName)
	namespace := r.Context().Value(types.NamespaceScope).(string)

	release, err := c.Repo().Release().ReadRelease(cluster.ID, name, namespace)
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
		return
	}

	res := &types.GetNotificationConfigResponse{
		NotificationConfig: &types.NotificationConfig{
			Enabled: true,
			Success: true,
			Failure: true,
		},
	}

	if release.NotificationConfig != 0 {
		notifConfig, err := c.Repo().NotificationConfig().ReadNotificationConfig(release.NotificationConfig)
		if err != nil {
			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
			return
		}

		res.NotificationConfig = notifConfig.ToNotificationConfigType()
	}

	c.WriteResult(w, r, res)
}
