package httpx

import "github.com/gin-gonic/gin"

type ErrorResponse struct {
	Error     string `json:"error"`
	RequestID string `json:"requestId,omitempty"`
}

func JSONError(c *gin.Context, status int, message string) {
	requestID, _ := c.Get("request_id")

	c.JSON(status, ErrorResponse{
		Error:     message,
		RequestID: toString(requestID),
	})
}

func JSON(c *gin.Context, status int, payload any) {
	c.JSON(status, payload)
}

func toString(value any) string {
	if value == nil {
		return ""
	}

	typed, ok := value.(string)
	if !ok {
		return ""
	}

	return typed
}
