package auth

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

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

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

	// 验证码操作
	router.GET("/captcha-image",
		middleware.RateLimit(middleware.LimitOption{
			Time:  300,
			Count: 60,
			Type:  middleware.LIMIT_IP,
		}),
		controller.NewCaptcha.Image,
	)

	// 账号身份操作
	account := controller.NewAccount
	accountGroup := router.Group("/auth")
	{
		accountGroup.POST("/login",
			middleware.RateLimit(middleware.LimitOption{
				Time:  180,
				Count: 15,
				Type:  middleware.LIMIT_IP,
			}),
			account.Login,
		)
		accountGroup.POST("/logout",
			middleware.RateLimit(middleware.LimitOption{
				Time:  120,
				Count: 15,
				Type:  middleware.LIMIT_IP,
			}),
			account.Logout,
		)
		accountGroup.POST("/refresh-token",
			middleware.RateLimit(middleware.LimitOption{
				Time:  60,
				Count: 5,
				Type:  middleware.LIMIT_IP,
			}),
			account.RefreshToken,
		)
		router.GET("/me",
			middleware.AuthorizeUser(nil),
			account.Me,
		)
		router.GET("/router",
			middleware.AuthorizeUser(nil),
			account.Router,
		)
	}

	// 登录认证源
	{
		accountGroup.GET("/login/source",
			middleware.RateLimit(middleware.LimitOption{
				Time:  300,
				Count: 60,
				Type:  middleware.LIMIT_IP,
			}),
			account.LoginSource,
		)
		accountGroup.POST("/login/ldap",
			middleware.RateLimit(middleware.LimitOption{
				Time:  180,
				Count: 15,
				Type:  middleware.LIMIT_IP,
			}),
			account.LDAP,
		)
		accountGroup.POST("/login/smtp",
			middleware.RateLimit(middleware.LimitOption{
				Time:  180,
				Count: 15,
				Type:  middleware.LIMIT_IP,
			}),
			account.SMTP,
		)
		accountGroup.GET("/login/oauth2/authorize",
			middleware.RateLimit(middleware.LimitOption{
				Time:  180,
				Count: 15,
				Type:  middleware.LIMIT_IP,
			}),
			account.OAuth2CodeURL,
		)
		accountGroup.POST("/login/oauth2/token",
			middleware.RateLimit(middleware.LimitOption{
				Time:  180,
				Count: 15,
				Type:  middleware.LIMIT_IP,
			}),
			account.OAuth2Token,
		)
	}

	// 账号注册操作
	{
		router.POST("/auth/register",
			middleware.RateLimit(middleware.LimitOption{
				Time:  300,
				Count: 10,
				Type:  middleware.LIMIT_IP,
			}),
			controller.NewRegister.Register,
		)
	}

	// 客户端授权管理
	oauth2Client := controller.NewOauth2Client
	oauth2ClientGroup := router.Group("/oauth2/client")
	{
		oauth2ClientGroup.GET("/list",
			middleware.AuthorizeUser(map[string][]string{"matchRoles": {"admin"}}),
			oauth2Client.List,
		)
		oauth2ClientGroup.GET("/:clientId",
			middleware.AuthorizeUser(map[string][]string{"matchRoles": {"admin"}}),
			oauth2Client.Info,
		)
		oauth2ClientGroup.POST("",
			middleware.AuthorizeUser(map[string][]string{"matchRoles": {"admin"}}),
			middleware.OperateLog(middleware.OptionNew("log.operate.title.oauth2client", middleware.BUSINESS_TYPE_INSERT)),
			oauth2Client.Add,
		)
		oauth2ClientGroup.PUT("",
			middleware.AuthorizeUser(map[string][]string{"matchRoles": {"admin"}}),
			middleware.OperateLog(middleware.OptionNew("log.operate.title.oauth2client", middleware.BUSINESS_TYPE_UPDATE)),
			oauth2Client.Edit,
		)
		oauth2ClientGroup.DELETE("/:id",
			middleware.AuthorizeUser(map[string][]string{"matchRoles": {"admin"}}),
			middleware.OperateLog(middleware.OptionNew("log.operate.title.oauth2client", middleware.BUSINESS_TYPE_DELETE)),
			oauth2Client.Remove,
		)
	}

	// 授权认证
	oauth2 := controller.NewOauth2
	oauth2Group := router.Group("/oauth2")
	{
		oauth2Group.GET("/authorize",
			middleware.RateLimit(middleware.LimitOption{
				Time:  60,
				Count: 30,
				Type:  middleware.LIMIT_IP,
			}),
			oauth2.Authorize,
		)
		oauth2Group.POST("/token",
			middleware.RateLimit(middleware.LimitOption{
				Time:  180,
				Count: 15,
				Type:  middleware.LIMIT_IP,
			}),
			oauth2.Token,
		)
		oauth2Group.POST("/refresh-token",
			middleware.RateLimit(middleware.LimitOption{
				Time:  60,
				Count: 5,
				Type:  middleware.LIMIT_IP,
			}),
			oauth2.RefreshToken,
		)
	}
}
