package initialize

import (
	"fmt"
	"sundynix-go/docs"
	"sundynix-go/global"
	"sundynix-go/middleware"
	"sundynix-go/router"

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"go.uber.org/zap"
)

// Routers 初始化总路由
func Routers() {
	Router := gin.New()

	Router.Use(gin.Recovery())
	if gin.Mode() == gin.DebugMode {
		Router.Use(gin.Logger())
	}
	docs.SwaggerInfo.BasePath = global.Config.System.RouterPrefix
	Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// 系统组路由
	systemRouter := router.GroupApp.System
	plantGroup := router.GroupApp.Plant

	NeedAuthGroup := Router.Group(global.Config.System.RouterPrefix)
	PublicGroup := Router.Group(global.Config.System.RouterPrefix)

	//鉴权中间件
	NeedAuthGroup.Use(middleware.AuthMiddleware())
	{
		//无须鉴权的路由
		systemRouter.InitAuthRouter(PublicGroup)   //登录不需要鉴权
		plantGroup.InitCallbackRouter(PublicGroup) // 回调
	}

	{
		//需要鉴权的路由
		systemRouter.InitUserRouter(NeedAuthGroup)        //用户相关
		systemRouter.InitClientRouter(NeedAuthGroup)      //客户端相关
		systemRouter.InitRoleRouter(NeedAuthGroup)        //角色相关
		systemRouter.InitMenuRouter(NeedAuthGroup)        //菜单相关
		systemRouter.InitOssRouter(NeedAuthGroup)         //OSS相关
		systemRouter.InitSysAiConfigRouter(NeedAuthGroup) //AI配置相关
	}

	{
		//需要鉴权的路由
		plantGroup.InitPlantRouter(NeedAuthGroup)       // 植物相关
		plantGroup.InitTopicRouter(NeedAuthGroup)       // 帖子话题
		plantGroup.InitPostRouter(NeedAuthGroup)        // 帖子相关
		plantGroup.InitWikiClassRouter(NeedAuthGroup)   //百科分类
		plantGroup.InitWikiRouter(NeedAuthGroup)        //百科
		plantGroup.InitOcrRouter(NeedAuthGroup)         // ocr识别
		plantGroup.InitLevelConfigRouter(NeedAuthGroup) //等级配置
		plantGroup.InitBadgeConfigRouter(NeedAuthGroup) //徽章配置
		plantGroup.InitUserProfileRouter(NeedAuthGroup) //用户资料
		plantGroup.InitExchangeRouter(NeedAuthGroup)    //兑换中心
		plantGroup.InitAiChatRouter(NeedAuthGroup)      //AI聊天

	}

	address := fmt.Sprintf(":%d", global.Config.System.Addr)
	fmt.Printf(`
	欢迎使用 sundynix-plant-go
	项目地址：
	默认自动化文档地址:http://127.0.0.1%s/swagger/index.html
	默认前端文件运行地址:http://127.0.0.1:8080
`, address)

	err := Router.Run(address)
	if err != nil {
		global.Logger.Error("Gin run failed", zap.Error(err))
	}
	global.Logger.Info("Gin run success", zap.String("address", address))

}
