package admin

import (
	"github.com/gin-gonic/gin"
	v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
	v1admin "github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
	"github.com/go-nunu/nunu-layout-advanced/internal/handler"
	"github.com/go-nunu/nunu-layout-advanced/internal/model"
	"github.com/go-nunu/nunu-layout-advanced/internal/service/admin"
	"net/http"
)

type GatewayIpAdminHandler struct {
	*handler.Handler
	gatewayIpAdminService admin.GatewayIpAdminService
}

func NewGatewayIpAdminHandler(
    handler *handler.Handler,
    gatewayIpAdminService admin.GatewayIpAdminService,
) *GatewayIpAdminHandler {
	return &GatewayIpAdminHandler{
		Handler:      handler,
		gatewayIpAdminService: gatewayIpAdminService,
	}
}

func (h *GatewayIpAdminHandler) GetGatewayIpAdmin(ctx *gin.Context) {
	var req v1admin.GatewayIp
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}

	res, err := h.gatewayIpAdminService.GetGatewayIpAdmin(ctx, int64(req.Id))
	if err != nil {
		v1.HandleError(ctx, http.StatusUnauthorized, v1.ErrUnauthorized, nil)
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *GatewayIpAdminHandler) GetGatewayIpAdminList(ctx *gin.Context) {
	var req v1admin.SearchGatewayIpParams
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	res, err := h.gatewayIpAdminService.GetGatewayGroupIpList(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *GatewayIpAdminHandler) AddGatewayIpAdmin(ctx *gin.Context) {
	var req model.Gatewayip
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	 err := h.gatewayIpAdminService.AddGatewayIp(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *GatewayIpAdminHandler) EditGatewayIpAdmin(ctx *gin.Context) {
	var req model.Gatewayip
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	err := h.gatewayIpAdminService.EditGatewayIp(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *GatewayIpAdminHandler) DeleteGatewayIpAdmin(ctx *gin.Context) {
	var req v1admin.GatewayIp
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	err := h.gatewayIpAdminService.DeleteGatewayIp(ctx, int64(req.Id))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *GatewayIpAdminHandler) DeleteGatewayIpsAdmin(ctx *gin.Context) {
	var req v1admin.DeleteGatewayIpRequest
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	err := h.gatewayIpAdminService.DeleteGatewayIps(ctx, req.Ids)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}