package initialize

import (
	"mtp20access/api/console"
	"mtp20access/global"
	"mtp20access/middleware"
	"mtp20access/router"

	docs "mtp20access/docs"

	"github.com/gin-gonic/gin"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
)

func Routers() *gin.Engine {
	Router := gin.Default()

	// 跨域，如需跨域可以打开下面的注释
	Router.Use(middleware.Cors()) // 直接放行全部跨域请求
	// Router.Use(middleware.CorsByRules()) // 按照配置的规则放行跨域请求
	//global.GVA_LOG.Info("use middleware cors")

	// 开发测试模式
	if global.M2A_CONFIG.System.Env == "develop" {
		docs.SwaggerInfo.BasePath = "/api"
		Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
		global.M2A_LOG.Info("register swagger handler")

		Router.GET("/debug/console", console.Console)
		Router.GET("/debug/update", console.MakeUpdateScript)
	}

	// 非鉴权组
	publicGroup := Router.Group("api")
	{
		// 开发测试模式
		if global.M2A_CONFIG.System.Env == "develop" {
			router.InitMQPublicRouter(publicGroup)
		}
		router.InitAccountPublicRouter(publicGroup)
		router.InitTradePublicRouter(publicGroup)
	}

	// 鉴权组
	privateGroup := Router.Group("api")
	privateGroup.Use(middleware.JWTAuth())
	{
		router.InitMQPrivateRouter(privateGroup)
		router.InitAccountPrivateRouter(privateGroup)
	}

	return Router
}
