package routers

import (
	"fmt"

	"git.apinb.com/bsm-sdk/core/middleware"
	"git.apinb.com/ops/sample/internal/logic/demo"
	"git.apinb.com/ops/sample/internal/logic/ping"
	"github.com/gin-gonic/gin"
)

// Register 注册路由，请求地址格式: ip:port/{srvKey}/v1/{group}/{action}
func Register(srvKey string, engine *gin.Engine) {
	v1Key := fmt.Sprintf("/%s/%s", srvKey, "v1")
	registerAnonymous(v1Key, engine)
	registerRouters(v1Key, engine)
}

func registerAnonymous(v1Key string, engine *gin.Engine) {
	anonymous := engine.Group(v1Key)
	{
		anonymous.GET("/ping/hello", ping.Hello)
	}
}

func registerRouters(v1Key string, engine *gin.Engine) {
	demoGroup := engine.Group(fmt.Sprintf("/%s/%s", v1Key, "demo"))
	demoGroup.Use(middleware.JwtAuth(true))
	{
		demoGroup.POST("", demo.Create)
		demoGroup.GET("", demo.List)
		demoGroup.GET("/:identity", demo.Get)
		demoGroup.PUT("/:identity", demo.Update)
		demoGroup.DELETE("/:identity", demo.Delete)
	}
}
