package common

import (
	"be.ems/src/framework/logger"
	"be.ems/src/framework/middleware"
	"be.ems/src/modules/common/controller"

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

// 模块路由注册
func Setup(router *gin.Engine) {
	logger.Infof("开始加载 ====> common 模块路由")

	// 路由主页
	router.GET("/",
		middleware.RateLimit(middleware.LimitOption{
			Time:  300,
			Count: 10,
			Type:  middleware.LIMIT_IP,
		}),
		controller.NewIndex.Handler,
	)

	// 路由服务器时间
	router.GET("/time",
		middleware.RateLimit(middleware.LimitOption{
			Time:  60,
			Count: 10,
			Type:  middleware.LIMIT_IP,
		}),
		controller.NewTimestamp.Handler,
	)

	// 通用请求
	commonGroup := router.Group("/common")
	{
		commonGroup.POST("/hash", middleware.AuthorizeUser(nil), controller.NewCommon.Hash)
		commonGroup.GET("/i18n", controller.NewCommon.I18n)
	}

	// 文件操作处理
	fileGroup := router.Group("/file")
	{
		fileGroup.POST("/upload", middleware.AuthorizeUser(nil), controller.NewFile.Upload)
		fileGroup.POST("/chunk-check", middleware.AuthorizeUser(nil), controller.NewFile.ChunkCheck)
		fileGroup.POST("/chunk-upload", middleware.AuthorizeUser(nil), controller.NewFile.ChunkUpload)
		fileGroup.POST("/chunk-merge", middleware.AuthorizeUser(nil), controller.NewFile.ChunkMerge)
		fileGroup.GET("/download/:filePath", middleware.AuthorizeUser(nil), controller.NewFile.Download)
		fileGroup.GET("/list", middleware.AuthorizeUser(nil), controller.NewFile.List)
		fileGroup.GET("", middleware.AuthorizeUser(nil), controller.NewFile.File)
		fileGroup.DELETE("", middleware.AuthorizeUser(nil), controller.NewFile.Remove)
		fileGroup.POST("/transfer-static-file", middleware.AuthorizeUser(nil), controller.NewFile.TransferStaticFile)
	}

	// 系统可暴露的配置信息
	router.GET("/sys-conf", controller.NewSysConf.Handler)

	// 系统引导初始化
	guideGroup := router.Group("/bootloader")
	{
		guideGroup.POST("", controller.NewBootloader.Start)
		guideGroup.PUT("", middleware.AuthorizeUser(nil), controller.NewBootloader.Done)
		guideGroup.DELETE("", middleware.AuthorizeUser(nil), controller.NewBootloader.Reset)
		guideGroup.PUT("/account", middleware.AuthorizeUser(nil), controller.NewBootloader.Account)
	}
}
