package invitationimp

import (
	"net/http"

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

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

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

	return sresponse.Invitation(a.Logger, inv)
}
