package apiresponse

import (
	"encoding/json"
	"net/http"
)

type Envelope struct {
	Code    int    `json:"code"`
	Message string `json:"msg"`
	Data    any    `json:"data,omitempty"`
}

func OK(writer http.ResponseWriter, data any) {
	Write(writer, http.StatusOK, Envelope{Code: http.StatusOK, Message: "success", Data: data})
}

func Error(writer http.ResponseWriter, status int, message string) {
	Write(writer, status, Envelope{Code: status, Message: message})
}

func Write(writer http.ResponseWriter, status int, value any) {
	writer.Header().Set("Content-Type", "application/json")
	writer.WriteHeader(status)
	_ = json.NewEncoder(writer).Encode(value)
}
