package common

import (
	"net/http"

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

type ResponseMessage struct {
	Code    int64  `json:"code,omitempty"`
	Message string `json:"message,omitempty"`
}

type BaseResp struct {
	Code    int    `json:"code,omitempty"`
	Message string `json:"message,omitempty"`
}

func FailWithCode(c *gin.Context, code int, msg string) {
	resp := &BaseResp{
		Code:    code,
		Message: msg,
	}
	c.JSON(code, resp)
}

func Success(c *gin.Context, resp any) {
	if resp == nil {
		resp = &BaseResp{Code: http.StatusOK, Message: ""}
	}
	c.JSON(ErrCodeOK.HTTPCode(), resp)
}

// Be careful to use the 401 error code
// Front end is sensitive to the 401 error code, and the corresponding operation is very heavy, direct exit login
func AuthError(c *gin.Context, message string) {
	c.JSON(http.StatusUnauthorized, &BaseResp{
		Code:    http.StatusUnauthorized,
		Message: message,
	})
	c.Abort()
}

func ForbiddenError(c *gin.Context, message string) {
	c.JSON(http.StatusForbidden, &BaseResp{
		Code:    http.StatusForbidden,
		Message: message,
	})
	c.Abort()
}
