package routes

import (
	"github.com/gofiber/fiber/v2"

	"github.com/break/junhong_cmp_fiber/internal/handler/admin"
	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	"github.com/break/junhong_cmp_fiber/pkg/constants"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/middleware"
	"github.com/break/junhong_cmp_fiber/pkg/openapi"
)

func registerWeComRoutes(router fiber.Router, handler *admin.WeComHandler, doc *openapi.Generator, basePath string) {
	group := router.Group("/wecom", func(c *fiber.Ctx) error {
		userType := middleware.GetUserTypeFromContext(c.UserContext())
		if userType != constants.UserTypeSuperAdmin && userType != constants.UserTypePlatform {
			return errors.New(errors.CodeForbidden, "无权限访问企业微信审批配置")
		}
		return c.Next()
	})
	groupPath := basePath + "/wecom"

	Register(group, doc, groupPath, "POST", "/applications", handler.Save, RouteSpec{
		Summary: "创建或更新企业微信应用配置",
		Tags:    []string{"企业微信审批"},
		Input:   new(dto.SaveWeComApplicationRequest),
		Output:  new(dto.WeComApplicationResponse),
		Auth:    true,
	})
	Register(group, doc, groupPath, "GET", "/applications", handler.List, RouteSpec{
		Summary: "查询企业微信应用配置",
		Tags:    []string{"企业微信审批"},
		Input:   new(dto.WeComApplicationListRequest),
		Output:  new(dto.WeComApplicationListResponse),
		Auth:    true,
	})
	Register(group, doc, groupPath, "POST", "/applications/:id/test", handler.Test, RouteSpec{
		Summary: "测试企业微信应用连接",
		Tags:    []string{"企业微信审批"},
		Input:   new(dto.IDReq),
		Output:  new(dto.WeComConnectionTestResponse),
		Auth:    true,
	})
	Register(group, doc, groupPath, "PUT", "/applications/:id/default-creator", handler.SaveDefaultCreator, RouteSpec{
		Summary: "保存企业微信应用默认审批发起人",
		Tags:    []string{"企业微信审批"},
		Input:   new(dto.SaveWeComDefaultCreatorParams),
		Output:  new(dto.WeComApplicationResponse),
		Auth:    true,
	})
	Register(group, doc, groupPath, "POST", "/applications/:id/members/sync", handler.SyncMembers, RouteSpec{
		Summary: "同步企业微信应用可见成员",
		Tags:    []string{"企业微信审批"},
		Input:   new(dto.IDReq),
		Output:  new(dto.WeComMemberSyncResponse),
		Auth:    true,
	})
	Register(group, doc, groupPath, "GET", "/applications/:id/members", handler.ListMembers, RouteSpec{
		Summary: "分页查询企业微信应用可见成员",
		Tags:    []string{"企业微信审批"},
		Input:   new(dto.WeComMemberListParams),
		Output:  new(dto.WeComMemberListResponse),
		Auth:    true,
	})
	Register(group, doc, groupPath, "PUT", "/scenes/:business_type", handler.SaveScene, RouteSpec{
		Summary: "保存并校验企业微信审批场景模板映射",
		Tags:    []string{"企业微信审批"},
		Input:   new(dto.SaveWeComApprovalSceneParams),
		Output:  new(dto.WeComApprovalSceneResponse),
		Auth:    true,
	})
	Register(group, doc, groupPath, "GET", "/scenes", handler.ListScenes, RouteSpec{
		Summary: "分页查询企业微信审批场景配置",
		Tags:    []string{"企业微信审批"},
		Input:   new(dto.WeComApprovalSceneListRequest),
		Output:  new(dto.WeComApprovalSceneListResponse),
		Auth:    true,
	})
}
