package invitationimp

import (
	"net/http"

	"github.com/tech/sendico/pkg/api/http/response"
	"github.com/tech/sendico/pkg/mutil/mzap"
	"go.uber.org/zap"
)

func (a *InvitationAPI) decline(r *http.Request) http.HandlerFunc {
	invitationRef, err := a.irh.GetRef(r)
	if err != nil {
		return a.respondBadReference(r, err)
	}

	ctx := r.Context()
	if err := a.db.Decline(ctx, invitationRef); err != nil {
		a.Logger.Warn("Failed to decline invitation", zap.Error(err), mzap.ObjRef("invitation_ref", invitationRef))
		return response.Auto(a.Logger, a.Name(), err)
	}

	return response.Success(a.Logger)
}
