package errs

import (
	"net/http"

	"gateway/internal/library/errors/code"

	"google.golang.org/grpc/codes"
)

// httpStatusFromGRPCDetail maps a stored gRPC codes.Code (in Detail) to HTTP.
// Used for CatGRPC errors produced by FromGRPCError.
func httpStatusFromGRPCDetail(d code.Detail) int {
	switch codes.Code(d) {
	case codes.OK:
		return http.StatusOK
	case codes.InvalidArgument, codes.OutOfRange, codes.FailedPrecondition:
		return http.StatusBadRequest
	case codes.NotFound:
		return http.StatusNotFound
	case codes.AlreadyExists, codes.Aborted:
		return http.StatusConflict
	case codes.PermissionDenied:
		return http.StatusForbidden
	case codes.Unauthenticated:
		return http.StatusUnauthorized
	case codes.ResourceExhausted:
		return http.StatusTooManyRequests
	case codes.DeadlineExceeded:
		return http.StatusGatewayTimeout
	case codes.Unavailable:
		return http.StatusServiceUnavailable
	case codes.Unimplemented:
		return http.StatusNotImplemented
	case codes.Canceled:
		return http.StatusRequestTimeout
	case codes.Unknown, codes.Internal, codes.DataLoss:
		return http.StatusInternalServerError
	default:
		return http.StatusInternalServerError
	}
}
