package pub

import (
	"git.apinb.com/bsm-sdk/core/crypto/token"
	"git.apinb.com/bsm-sdk/core/env"
	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/infra"
	"git.apinb.com/bsm-sdk/core/service"
	"github.com/gin-gonic/gin"
)

// Refresh 刷新token
// @Summary 刷新token
// @Description 刷新token
// @Tags 刷新token
// @Accept application/json
// @Produce application/json
// @Security ApiKeyAuth
// @Success 200 {object} coreTypes.LoginResp "刷新成功,返回token"
// @Router /pub/refresh [post]
func Refresh(c *gin.Context) {
	claims, err := service.ParseMetaCtx(c, nil)
	if err != nil {
		infra.Response.Error(c, err)
		return
	}
	// 将 ExpiresAt 转换为 time.Time 类型
	token, err := token.New(env.Runtime.JwtSecretKey).GenerateJwt(claims.ID, claims.Identity, claims.Client, claims.Role, nil, claims.Extend)
	if err != nil {
		infra.Response.Error(c, errcode.ErrTokenGenerate)
		return
	}

	infra.Response.Success(c, map[string]string{"token": token})
}
