package accountapiimp

import (
	"errors"
	"net/http"

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

func (a *AccountAPI) mapTokenErrorToResponse(err error) http.HandlerFunc {
	if errors.Is(err, verification.ErrTokenNotFound) {
		a.logger.Debug("Verification token not found during consume", zap.Error(err))
		return response.NotFound(a.logger, a.Name(), "No account found associated with given verifcation token")
	}
	if errors.Is(err, verification.ErrTokenExpired) {
		a.logger.Debug("Verification token expired during consume", zap.Error(err))
		return response.Gone(a.logger, a.Name(), "token_expired", "verification token has expired")
	}
	if errors.Is(err, verification.ErrTokenAlreadyUsed) {
		a.logger.Debug("Verification token already used during consume", zap.Error(err))
		return response.DataConflict(a.logger, a.Name(), "verification token has already been used")
	}
	if err != nil {
		a.logger.Warn("Uenxpected error during token verification", zap.Error(err))
		return response.Auto(a.logger, a.Name(), err)
	}
	a.logger.Debug("No token verification error found")
	return response.Success(a.logger)
}
