package web

import (
	"log"

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

// render is a wrapper function that decides how to render a response, renders
// the response, and can be extended to include additional tasks when rendering
// for debug purposes, such as logging failures and contextually including extra
// information.
func render(c *gin.Context, status int, data interface{}) {
	var renderer r.Render

	if gin.IsDebugging() {
		renderer = r.IndentedJSON{Data: data}
	} else {
		renderer = r.JSON{Data: data}
	}

	if status > 400 {
		log.Printf("Failed to process request\tstatus: %d\tdata: %+v", status, data)
	}

	c.Render(status, renderer)
}
