package proxy

import (
	"errors"
	"net/http"
	"strings"

	"github.com/rose_cat707/luminary/internal/gateway"
)

func gatewayHTTPStatus(err error) int {
	switch {
	case errors.Is(err, gateway.ErrRateLimitExceeded):
		return http.StatusTooManyRequests
	case errors.Is(err, gateway.ErrModelNotAllowed),
		errors.Is(err, gateway.ErrBudgetExceeded):
		return http.StatusForbidden
	case errors.Is(err, gateway.ErrInvalidJSON),
		errors.Is(err, gateway.ErrModelRequired):
		return http.StatusBadRequest
	}
	msg := err.Error()
	if strings.Contains(msg, "not enabled") || strings.Contains(msg, "not allowed") {
		return http.StatusForbidden
	}
	if strings.Contains(msg, "invalid api key") || strings.Contains(msg, "expired") {
		return http.StatusUnauthorized
	}
	return http.StatusBadGateway
}
