package web

import (
	"github.com/gogf/gf/v2/net/ghttp"
	"go.uber.org/dig"
	"yx-dataset-server/library/auth"
	"yx-dataset-server/library/middleware"
	"yx-dataset-server/router/web/controllers"
)

// 注册路由
func RegisterRouters(s *ghttp.Server, container *dig.Container) error {
	controllers.Inject(container)
	gr := s.Group("/web")
	// 注册请求限制中间件

	return container.Invoke(func(
		auth auth.Auther,
		cOrganization *controllers.Organization,

		cUser *controllers.User,
		cDataset *controllers.Dataset,
		cDatasetFile *controllers.DatasetFile,

		cMenu *controllers.Menu,
		cRole *controllers.Role,

		cLogin *controllers.Login,
		cFile *controllers.File,

		cChatAssistant *controllers.ChatAssistant,
		cChatMessage *controllers.ChatMessage,
		cChatSession *controllers.ChatSession,
		cRobotConfig *controllers.RobotConfig,

	) {
		gr.Middleware(middleware.UserAuthMiddleware(auth,
			middleware.AllowMethodAndPathPrefixSkipper(
				middleware.JoinRouter("GET", "/web/v1/login"),
				middleware.JoinRouter("POST", "/web/v1/login"),
			),
		))
		v1 := gr.Group("/v1")
		{
			// 登录
			gLogin := v1.Group("/login")
			{
				gLogin.GET("/captchaid", cLogin.GetCaptchaID)
				gLogin.GET("/captcha", cLogin.GetCaptcha)
				gLogin.POST("/", cLogin.Login)
				gLogin.POST("/exit", cLogin.Logout)
				gLogin.POST("/code", cLogin.Send)
				gLogin.POST("/sms", cLogin.LoginBySms)
				gLogin.PUT("/refresh/token", cLogin.RefreshToken)
			}

			v1.GET("/current/user", cLogin.GetUserInfo)
			v1.PUT("/current/user", cLogin.UpdatePassword)
			v1.POST("/files", cFile.Upload)

			gOrganization := v1.Group("/organizations")
			{
				gOrganization.GET("/", cOrganization.Query)
				gOrganization.GET("/:id", cOrganization.Get)
				gOrganization.POST("/", cOrganization.Create)
				gOrganization.PUT("/:id", cOrganization.Update)
				gOrganization.DELETE("/:id", cOrganization.Delete)
			}

			gUser := v1.Group("/users")
			{
				gUser.GET("/", cUser.Query)
				gUser.GET("/:id", cUser.Get)
				gUser.POST("/", cUser.Create)
				gUser.PUT("/:id", cUser.Update)
				gUser.DELETE("/:id", cUser.Delete)
				gUser.PATCH("/:id/enable", cUser.Enable)
				gUser.PATCH("/:id/disable", cUser.Disable)
			}

			gDataset := v1.Group("/datasets")
			{
				gDataset.GET("/", cDataset.Query)
				gDataset.GET("/:id", cDataset.Get)
				gDataset.GET("/permission", cDataset.GetPermissionDatasets)
				gDataset.GET("/available", cDataset.GetAvailableDatasets)
				gDataset.POST("/", cDataset.Create)
				gDataset.PUT("/:id", cDataset.Update)
				gDataset.DELETE("/:id", cDataset.Delete)
			}

			gDatasetFile := v1.Group("/dataset_files")
			{
				gDatasetFile.GET("/", cDatasetFile.Query)
				gDatasetFile.GET("/:id", cDatasetFile.Get)
				gDatasetFile.POST("/", cDatasetFile.Create)
				gDatasetFile.POST("/batch", cDatasetFile.BatchCreate)
				gDatasetFile.PUT("/:id", cDatasetFile.Update)
				gDatasetFile.DELETE("/:id", cDatasetFile.Delete)
				gDatasetFile.POST("/batch_delete", cDatasetFile.BatchDelete)
				gDatasetFile.PATCH("/:id/enabled", cDatasetFile.Enabled)
				gDatasetFile.PATCH("/:id/disabled", cDatasetFile.Disabled)
				gDatasetFile.POST("/batch/v2", cDatasetFile.BatchCreateV2)
				gDatasetFile.POST("/upload", cDatasetFile.CreateV2)
			}

			gMenu := v1.Group("/menus")
			{
				gMenu.GET("/", cMenu.Query)
				gMenu.GET("/:id", cMenu.Get)
				gMenu.POST("/", cMenu.Create)
				gMenu.PUT("/:id", cMenu.Update)
				gMenu.PUT("/:id/sequence", cMenu.UpdateSequence)
				gMenu.DELETE("/:id", cMenu.Delete)
			}
			gRole := v1.Group("/roles")
			{
				gRole.GET("/", cRole.Query)
				gRole.GET("/:id", cRole.Get)
				gRole.POST("/", cRole.Create)
				gRole.PUT("/:id", cRole.Update)
				gRole.DELETE("/:id", cRole.Delete)
			}

			gChatAssistant := v1.Group("/chat_assistants")
			{
				gChatAssistant.GET("/", cChatAssistant.Query)
				gChatAssistant.GET("/permission/check", cChatAssistant.CheckPermissionSync)
				gChatAssistant.POST("/permission/sync", cChatAssistant.SyncPermission)
				gChatAssistant.GET("/:id", cChatAssistant.Get)
				gChatAssistant.POST("/", cChatAssistant.Create)
				gChatAssistant.PUT("/:id", cChatAssistant.Update)
				gChatAssistant.DELETE("/:id", cChatAssistant.Delete)
			}
			gChatSession := v1.Group("/chat_sessions")
			{
				gChatSession.GET("/", cChatSession.Query)
				gChatSession.GET("/:id", cChatSession.Get)
				gChatSession.POST("/", cChatSession.Create)
				gChatSession.PUT("/:id", cChatSession.Update)
				gChatSession.DELETE("/:id", cChatSession.Delete)
			}
			gChatMessage := v1.Group("/chat_messages")
			{
				gChatMessage.GET("/", cChatMessage.Query)
				gChatMessage.GET("/:id", cChatMessage.Get)
				//gChatMessage.POST("/", cChatMessage.Create)
				gChatMessage.PUT("/:id", cChatMessage.Update)
				gChatMessage.DELETE("/:id", cChatMessage.Delete)
			}

			gRobot := v1.Group("/robot_configs")
			{
				gRobot.GET("/", cRobotConfig.Query)
				gRobot.GET("/:id", cRobotConfig.Get)
				gRobot.POST("/", cRobotConfig.Create)
				gRobot.PUT("/:id", cRobotConfig.Update)
				gRobot.DELETE("/:id", cRobotConfig.Delete)
			}
		}
		v2 := gr.Group("/v2")
		{
			gDatasetFile := v2.Group("/dataset_files")
			{
				gDatasetFile.POST("/", cDatasetFile.CreateV2)
				gDatasetFile.POST("/batch", cDatasetFile.BatchCreateV2)
			}
		}
	})
}
