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"
	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	"github.com/break/junhong_cmp_fiber/pkg/openapi"
)

// registerRoleRoutes 注册角色相关路由
func registerRoleRoutes(api fiber.Router, h *admin.RoleHandler, doc *openapi.Generator, basePath string) {
	roles := api.Group("/roles")
	groupPath := basePath + "/roles"

	// 角色 CRUD
	Register(roles, doc, groupPath, "POST", "", h.Create, RouteSpec{
		Summary: "创建角色",
		Tags:    []string{"角色"},
		Input:   new(dto.CreateRoleRequest),
		Output:  new(dto.RoleResponse),
		Auth:    true,
	})

	Register(roles, doc, groupPath, "GET", "", h.List, RouteSpec{
		Summary: "角色列表",
		Tags:    []string{"角色"},
		Input:   new(dto.RoleListRequest),
		Output:  new(dto.RolePageResult),
		Auth:    true,
	})

	Register(roles, doc, groupPath, "GET", "/:id", h.Get, RouteSpec{
		Summary: "获取角色详情",
		Tags:    []string{"角色"},
		Input:   new(dto.IDReq),
		Output:  new(dto.RoleResponse),
		Auth:    true,
	})

	Register(roles, doc, groupPath, "PUT", "/:id", h.Update, RouteSpec{
		Summary: "更新角色",
		Tags:    []string{"角色"},
		Input:   new(dto.UpdateRoleParams),
		Output:  new(dto.RoleResponse),
		Auth:    true,
	})

	Register(roles, doc, groupPath, "PUT", "/:id/status", h.UpdateStatus, RouteSpec{
		Summary: "更新角色状态",
		Tags:    []string{"角色"},
		Input:   new(dto.UpdateRoleStatusParams),
		Output:  nil,
		Auth:    true,
	})

	Register(roles, doc, groupPath, "PUT", "/:id/default-credit", h.UpdateDefaultCredit, RouteSpec{
		Summary: "更新客户角色的新建代理默认信用模板",
		Tags:    []string{"角色"},
		Input:   new(dto.UpdateRoleDefaultCreditParams),
		Output:  new(dto.RoleDefaultCreditResponse),
		Auth:    true,
	})

	Register(roles, doc, groupPath, "DELETE", "/:id", h.Delete, RouteSpec{
		Summary: "删除角色",
		Tags:    []string{"角色"},
		Input:   new(dto.IDReq),
		Output:  nil,
		Auth:    true,
	})

	// 角色-权限关联
	Register(roles, doc, groupPath, "POST", "/:id/permissions", h.AssignPermissions, RouteSpec{
		Summary: "分配权限",
		Tags:    []string{"角色"},
		Input:   new(dto.AssignPermissionsParams),
		Output:  nil,
		Auth:    true,
	})

	Register(roles, doc, groupPath, "GET", "/:id/permissions", h.GetPermissions, RouteSpec{
		Summary: "获取角色权限",
		Tags:    []string{"角色"},
		Input:   new(dto.IDReq),
		Output:  new([]model.Permission),
		Auth:    true,
	})

	Register(roles, doc, groupPath, "DELETE", "/:role_id/permissions/:perm_id", h.RemovePermission, RouteSpec{
		Summary: "移除权限",
		Tags:    []string{"角色"},
		Input:   new(dto.RemovePermissionParams),
		Output:  nil,
		Auth:    true,
	})

	Register(roles, doc, groupPath, "DELETE", "/:role_id/permissions", h.BatchRemovePermissions, RouteSpec{
		Summary: "批量移除权限",
		Tags:    []string{"角色"},
		Input:   new(dto.BatchRemovePermissionsParams),
		Body:    new(dto.BatchRemovePermissionsRequest),
		Output:  nil,
		Auth:    true,
	})
}
