package errors

import (
	"net/http"

	"github.com/labstack/echo/v4"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
)

var httpStatuses = map[ErrorType]int{
	ErrInternalError:        http.StatusInternalServerError,
	ErrInvalidArgs:          http.StatusBadRequest,
	ErrNoAccess:             http.StatusForbidden,
	ErrNotFound:             http.StatusNotFound,
	ErrMethodNotImplemented: http.StatusNotImplemented,
	ErrConflict:             http.StatusConflict,
	ErrInsufficientFunds:    http.StatusPaymentRequired,
}

func HTTP(ctx echo.Context, err Error) error {
	status, ok := httpStatuses[err.Type()]
	if !ok {
		return ctx.JSON(http.StatusInternalServerError, models.ResponseErrorHTTP{
			StatusCode: http.StatusInternalServerError,
			Message:    err.Error(),
		})
	}

	return ctx.JSON(status, models.ResponseErrorHTTP{
		StatusCode: status,
		Message:    err.Error(),
	})
}
