package utils

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/rs/zerolog/log"
)

const APIVersion = "v1"

func AssembleResponse(c *gin.Context, data interface{}, err error, opts ...ResponseDataOptionFunc) {
	code := http.StatusOK
	if err != nil {
		log.Error().Err(err)
		code = http.StatusInternalServerError
		c.JSON(code, FailResponse{
			APIVersion: APIVersion,
			Code:       "ERROR",
			Message:    err.Error(),
			StatusCode: code,
		})
		return
	}
	resp := SuccessResponse{
		APIVersion: APIVersion,
		Code:       "OK",
		Message:    "success",
		StatusCode: code,
		Data:       data,
	}

	for _, opt := range opts {
		opt(&resp)
	}

	c.JSON(code, resp)
}
