package controller

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/swagger"
	"gofiber.study.skcks.cn/common/errorx"
	"gofiber.study.skcks.cn/controller/auth"
	"gofiber.study.skcks.cn/controller/casbin"
	"gofiber.study.skcks.cn/controller/test"
	"gofiber.study.skcks.cn/controller/types"
	"gofiber.study.skcks.cn/controller/user"
	"reflect"
)

func RegisterController(app *fiber.App) {
	controllers := []types.ControllerInterface{
		auth.NewController(app),
		user.NewController(app),
		casbin.NewController(app),
		test.NewController(app),
	}

	args := make([]reflect.Value, 0)
	for _, controller := range controllers {
		reflectRegisterRoute(controller, args)
	}
}

func reflectRegisterRoute(controller interface{}, args []reflect.Value) {
	t := reflect.ValueOf(controller)
	for i := 0; i < t.NumMethod(); i++ {
		method := t.Method(i)
		method.Call(args)
	}
}

// ErrorHandler
//
// @Summary      错误处理
// @Description  错误信息示例
// @Tags         Error
// @Accept       json
// @Produce      json
// @Failure	     default {object}  errorx.CodeErrorResponse
// @Router       /error [get]
// @Router       /error [post]
// @Router       /error [put]
// @Router       /error [delete]
// @Router       /error [patch]
func ErrorHandler(app *fiber.App) {
	app.All("/error", func(ctx *fiber.Ctx) error {
		return ctx.JSON(errorx.NewDefaultError("错误信息示例"))
	})
}

func SwaggerHandler(app *fiber.App) {
	app.Get("/swagger/*", swagger.New(swagger.Config{
		DeepLinking: false,
		// Expand ("list") or Collapse ("none") tag groups by default
		DocExpansion: "list",
	}))
}
