package utils

import (
	"net/http"

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

// JSON writes a JSON response with the given status.
func JSON(c *gin.Context, status int, body any) {
	c.JSON(status, body)
}

// ErrorPayload is a small stable shape for handler errors.
type ErrorPayload struct {
	Error   string `json:"error"`
	TraceID string `json:"trace_id,omitempty"`
}

// AbortWithErrorJSON aborts the chain with a JSON error body.
func AbortWithErrorJSON(c *gin.Context, status int, traceID, msg string) {
	c.AbortWithStatusJSON(status, ErrorPayload{Error: msg, TraceID: traceID})
}

// AbortWithValidation aborts with 400 for validation failures.
func AbortWithValidation(c *gin.Context, traceID string, err error) {
	AbortWithErrorJSON(c, http.StatusBadRequest, traceID, err.Error())
}
