package tmobile

import (
	"github.com/pkg/errors"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

var ErrCreateRequest = errors.New("failed to create request")
var ErrDoRequest = errors.New("failed to do request")
var ErrReadResponseBody = errors.New("failed to read response body")
var ErrBadStatusCode = errors.New("bad status code")
var ErrBadGatewayCode = errors.New("bad gateway timeout")
var ErrJSONMarshal = errors.New("json marshal & unmarshal error")
var ErrTokenGen = errors.New("failed to generate token")
var ErrBadMsgStatus = errors.New("bad message status")
var ErrTimeoutSendingMessage = errors.New("timeout sending message")
var ErrAccessTokenExpired = errors.New("access token expired")

func ErrorToGRPCError(err error) error {
	if err == nil {
		return nil
	}

	if errors.Is(err, ErrCreateRequest) {
		return status.Errorf(codes.Internal, "%s", err.Error())
	}

	if errors.Is(err, ErrDoRequest) {
		return status.Errorf(codes.Internal, "%s", err.Error())
	}

	if errors.Is(err, ErrReadResponseBody) {
		return status.Errorf(codes.Internal, "%s", err.Error())
	}

	if errors.Is(err, ErrBadStatusCode) {
		return status.Errorf(codes.Internal, "%s", err.Error())
	}

	if errors.Is(err, ErrBadGatewayCode) {
		return status.Errorf(codes.Internal, "%s", err.Error())
	}

	if errors.Is(err, ErrJSONMarshal) {
		return status.Errorf(codes.Internal, "%s", err.Error())
	}

	if errors.Is(err, ErrTokenGen) {
		return status.Errorf(codes.Internal, "%s", err.Error())
	}

	if errors.Is(err, ErrBadMsgStatus) {
		return status.Errorf(codes.Internal, "%s", err.Error())
	}

	if errors.Is(err, ErrTimeoutSendingMessage) {
		return status.Errorf(codes.DeadlineExceeded, "%s", err.Error())
	}

	return status.Errorf(codes.Unknown, "unknown error: %v", err)
}
