package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/mhaya/game/game_cluster/internal/code"
	"github.com/mhaya/game/game_cluster/nodes/webadmin/common"
	"github.com/mhaya/game/game_cluster/nodes/webadmin/entity"
	"github.com/mhaya/game/game_cluster/nodes/webadmin/service"
)

type Whitelist struct {
	sev *service.Whitelist
}

func NewWhitelist() *Whitelist {
	return &Whitelist{
		sev: service.NewWhitelist(),
	}
}

// GetWhitelist 获取白名单
// @Summary 获取白名单
// @Description 获取白名单
// @Tags 白名单
// @Accept json
// @Produce json
// @Param req body entity.WhitelistListReq true "请求参数"
// @Success 200 {object} []models.Whitelist "返回结果"
// @Router /v1/admin/whitelist/list [post]
func (w *Whitelist) GetWhitelist(ctx *gin.Context) {
	var req entity.WhitelistListReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.PackOkResult(ctx, code.ParamError)
		return
	}

	resp, err := w.sev.GetAll(req)
	if err != nil {
		common.PackOkResult(ctx, err.Code)
		return
	}

	common.PackOkResult(ctx, code.OK, resp)
}

// AddWhitelist 新增白名单
// @Summary 新增白名单
// @Description 新增白名单
// @Tags 白名单
// @Accept json
// @Produce json
// @Param req body entity.WhitelistAddReq true "请求参数"
// @Success 200 {object} entity.Response "返回结果"
// @Router /v1/admin/whitelist/add [post]
func (w *Whitelist) AddWhitelist(ctx *gin.Context) {
	var req entity.WhitelistAddReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.PackOkResult(ctx, code.ParamError)
		return
	}

	err := w.sev.Add(req)
	if err != nil {
		common.PackOkResult(ctx, err.Code)
		return
	}

	common.PackOkResult(ctx, code.OK, nil)
}

// RemoveWhitelist 删除白名单
// @Summary 删除白名单
// @Description 删除白名单
// @Tags 白名单
// @Accept json
// @Produce json
// @Param req body entity.WhitelistRemoveReq true "请求参数"
// @Success 200 {object} entity.Response "返回结果"
// @Router /v1/admin/whitelist/remove [post]
func (w *Whitelist) RemoveWhitelist(ctx *gin.Context) {
	var req entity.WhitelistRemoveReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.PackOkResult(ctx, code.ParamError)
		return
	}

	err := w.sev.Remove(req)
	if err != nil {
		common.PackOkResult(ctx, err.Code)
		return
	}

	common.PackOkResult(ctx, code.OK, nil)
}
