package routers

import (
	"bsm/full/module/base/mgt/internal/logic/application"
	"bsm/full/module/base/mgt/internal/logic/department"
	"bsm/full/module/base/mgt/internal/logic/hello"
	"bsm/full/module/base/mgt/internal/logic/permission"
	"bsm/full/module/base/mgt/internal/logic/pub"
	"bsm/full/module/base/mgt/internal/logic/role"
	"bsm/full/module/base/mgt/internal/logic/user"
	mgtmw "bsm/full/module/base/mgt/internal/middleware"
	"git.apinb.com/bsm-sdk/core/middleware"
	"github.com/gin-gonic/gin"
)

// 完成请求地址: ip:port/srvKey/group/xxx
func Register(srvKey string, engine *gin.Engine) {
	base := "/" + srvKey
	registerAnonymous(base, engine)
	registerRouters(base, engine)
}

// registerAnonymous  不需要auth的接口
func registerAnonymous(base string, engine *gin.Engine) {
	anonymous := engine.Group(base)
	{
		// anonymous.Use(middleware.JwtAuth())
		anonymous.GET("/ping", hello.Ping)
		anonymous.GET("/session", hello.SessionDemo)

		anonymous.POST("/login", pub.Login)
		anonymous.POST("/refresh", pub.Refresh)
		anonymous.POST("/reset", pub.ForgetPwdBySms) // 重置密码

	}
}

func registerRouters(base string, engine *gin.Engine) {
	auth := middleware.JwtAuth(true)
	admin := mgtmw.RequireAdmin()

	// 用户
	userGroup := engine.Group(base + "/user")
	userGroup.Use(auth)
	{
		// 本人可查
		userGroup.POST("/role", user.RoleFetch)
		userGroup.POST("/app", user.ApplicationsFetch)
		userGroup.POST("/pmn", user.PermissionsFetch)
		userGroup.POST("/pmn_tree", user.PermissionsFetchTree)

		adminUser := userGroup.Group("")
		adminUser.Use(admin)
		adminUser.POST("/create", user.Create)
		adminUser.POST("/del", user.Del)
		adminUser.POST("/detail", user.Detail)
		adminUser.POST("/modify", user.Midify)
		adminUser.POST("/fetch", user.Fetch)
		adminUser.POST("/list", user.UserList)
		adminUser.POST("/set_role", user.SetRole)
		adminUser.POST("/del_role", user.DelRole)
		adminUser.POST("/set_pmn", user.SetPmn)
		adminUser.POST("/modify_pmn", user.ModifyPmn)
		adminUser.POST("/del_pmn", user.DelPmn)
	}

	// 应用
	appGroup := engine.Group(base + "/app")
	appGroup.Use(auth, admin)
	{
		appGroup.POST("/create", application.Create)
		appGroup.POST("/del", application.Del)
		appGroup.POST("/detail", application.Detail)
		appGroup.POST("/modify", application.Midify)
		appGroup.POST("/fetch", application.Fetch)
		appGroup.POST("/user", application.UserFetch)
		appGroup.POST("/role", application.RoleFetch)
		appGroup.POST("/pmn", application.PermissionFetch)
	}

	// 角色
	roleGroup := engine.Group(base + "/role")
	roleGroup.Use(auth, admin)
	{
		roleGroup.POST("/create", role.Create)
		roleGroup.POST("/del", role.Del)
		roleGroup.POST("/detail", role.Detail)
		roleGroup.POST("/modify", role.Midify)
		roleGroup.POST("/fetch", role.Fetch)
		roleGroup.POST("/user", role.UserFetch)
		roleGroup.POST("/app", role.ApplicationsFetch)
		roleGroup.POST("/set_pmn", role.SetPmn)
		roleGroup.POST("/modify_pmn", role.ModifyPmn)
		roleGroup.POST("/del_pmn", role.DelPmn)
		roleGroup.POST("/pmn", role.PermissionsFetch)
		roleGroup.POST("/pmn_tree", role.PermissionsFetchTree)
	}

	// 权限
	pmnGroup := engine.Group(base + "/pmn")
	pmnGroup.Use(auth, admin)
	{
		pmnGroup.POST("/create", permission.Create)
		pmnGroup.POST("/del", permission.Del)
		pmnGroup.POST("/detail", permission.Detail)
		pmnGroup.POST("/modify", permission.Midify)
		pmnGroup.POST("/fetch", permission.Fetch)
		pmnGroup.POST("/sort", permission.Sort)
		pmnGroup.POST("/user", permission.UserFetch)
		pmnGroup.POST("/role", permission.RoleFetch)
	}

	// 部门
	dptGroup := engine.Group(base + "/dpt")
	dptGroup.Use(auth, admin)
	{
		dptGroup.POST("/create", department.Create)
		dptGroup.POST("/del", department.Del)
		dptGroup.POST("/detail", department.Detail)
		dptGroup.POST("/modify", department.Modify)
		dptGroup.POST("/fetch", department.Fetch)
		dptGroup.POST("/list", department.List)
		dptGroup.POST("/fetch_tree", department.FetchTree)
		dptGroup.POST("/set_pmn", department.SetPmn)
		dptGroup.POST("/modify_pmn", department.ModifyPmn)
		dptGroup.POST("/del_pmn", department.DelPmn)
		dptGroup.POST("/pmn", department.PermissionsFetch)
		dptGroup.POST("/pmn_tree", department.PermissionsFetchTree)
		dptGroup.POST("/set_user", department.SetUser)
		dptGroup.POST("/del_user", department.DelUser)
		dptGroup.POST("/user", department.UserFetch)
	}
}
