package api

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"github.com/photoprism/photoprism/internal/auth/acl"
	"github.com/photoprism/photoprism/internal/form"
	"github.com/photoprism/photoprism/internal/photoprism/get"
	"github.com/photoprism/photoprism/pkg/i18n"
)

// SendFeedback allows members to submit a feedback message to the PhotoPrism team.
//
//	@Summary	allows members to submit a feedback message to the PhotoPrism team
//	@Id			SendFeedback
//	@Tags		Internal
//	@Produce	json
//	@Success	200			{object}	form.Feedback
//	@Failure	400,401,403	{object}	i18n.Response
//	@Router		/api/v1/feedback [post]
func SendFeedback(router *gin.RouterGroup) {
	router.POST("/feedback", func(c *gin.Context) {
		conf := get.Config()

		if conf.Public() {
			Abort(c, http.StatusForbidden, i18n.ErrPublic)
			return
		}

		s := Auth(c, acl.ResourceFeedback, acl.ActionCreate)

		// Abort if permission is not granted.
		if s.Abort(c) {
			return
		}

		conf.RenewApiKeys()

		var frm form.Feedback

		// Assign and validate request form values.
		if err := c.BindJSON(&frm); err != nil {
			AbortBadRequest(c, err)
			return
		}

		if frm.Empty() {
			Abort(c, http.StatusBadRequest, i18n.ErrNoItemsSelected)
			return
		}

		if err := conf.Hub().SendFeedback(frm); err != nil {
			log.Error(err)
			AbortSaveFailed(c)
			return
		}

		c.JSON(http.StatusOK, gin.H{"code": http.StatusOK})
	})
}
