package handler

import (
	"github.com/gin-gonic/gin"
	v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
	"github.com/go-nunu/nunu-layout-advanced/internal/service"
	"github.com/mcuadros/go-defaults"
	"net/http"
)

type GatewayGroupHandler struct {
	*Handler
	gatewayGroupService service.GatewayGroupService
}

func NewGatewayGroupHandler(
	handler *Handler,
	gatewayGroupService service.GatewayGroupService,
) *GatewayGroupHandler {
	return &GatewayGroupHandler{
		Handler:             handler,
		gatewayGroupService: gatewayGroupService,
	}
}

func (h *GatewayGroupHandler) GetGatewayGroup(ctx *gin.Context) {

}

func (h *GatewayGroupHandler) GetGatewayGroupList(ctx *gin.Context) {
	req := new(v1.SearchGatewayGroupParams)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
	}

	res, err := h.gatewayGroupService.GetGatewayGroupList(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}

	v1.HandleSuccess(ctx, res)

}

func (h *GatewayGroupHandler) AddGatewayGroup(ctx *gin.Context) {
	var req v1.AddGateWayGroupAdminRequest

	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err)
		return
	}
	defaults.SetDefaults(&req)
	 err := h.gatewayGroupService.AddGatewayGroupAdmin(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError,err, nil)
		return
	}

	v1.HandleSuccess(ctx, nil)

}

func (h *GatewayGroupHandler) EditGatewayGroup(ctx *gin.Context) {
	var req v1.AddGateWayGroupAdminRequest

	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err)
		return
	}
	defaults.SetDefaults(&req)
	 err := h.gatewayGroupService.EditGatewayGroupAdmin(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err,nil)
		return
	}

	v1.HandleSuccess(ctx, nil)

}

func (h *GatewayGroupHandler) DeleteGatewayGroup(ctx *gin.Context) {
	var req v1.DeleteGatewayGroupRequest

	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err)
		return
	}
	err := h.gatewayGroupService.DeleteGatewayGroup(ctx, req.Id)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}

	v1.HandleSuccess(ctx, nil)

}