package help

import (
	"mini_server/internal/config"
	"mini_server/internal/middleware/jwt"

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

func GetUserId(c *gin.Context) (userId int32, ok bool) {
	defer func() {
		if !ok && config.UseDefaultUserId {
			userId = config.DefaultUserId
			ok = true
		}
	}()

	v, ok := c.Get("user")
	logrus.Tracef("GetUserInfo, path:%v,method:%v,user v:%#v", c.Request.URL.Path, c.Request.Method, v)
	if !ok {
		return
	}
	cliams, ok := v.(*jwt.Claims)
	if !ok {
		return
	}
	if cliams.ID == int32(0) {
		return
	}
	userId = cliams.ID
	ok = true
	return
}
