package utils

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"gitlab.com/security-rd/go-pkg/logging"
)

const APIVersion = "v1"

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

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

	c.JSON(code, resp)
}
