package permissionsimp

import (
	"net/http"

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

func (a *PermissionsAPI) deleteRoleDescription(r *http.Request, account *model.Account, _ *sresponse.TokenData) http.HandlerFunc {
	roleDescriptionRef, err := a.Rph.GetRef(r)
	if err != nil {
		a.logger.Warn("Failed to restore object reference", zap.Error(err), mutil.PLog(a.Rph, r))
		return response.BadReference(a.logger, a.Name(), a.Rph.Name(), a.Rph.GetID(r), err)
	}

	if err := a.rdb.Delete(r.Context(), roleDescriptionRef); err != nil {
		a.logger.Warn("Failed to delete role description", zap.Error(err),
			mzap.ObjRef("requesting_account_ref", account.ID), mzap.ObjRef("role_ref", roleDescriptionRef))
		return response.Auto(a.logger, a.Name(), err)
	}

	return response.Success(a.logger)
}
