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"
	"net/http"
)

type AllowAndDenyIpHandler struct {
	*Handler
	allowAndDenyIpService service.AllowAndDenyIpService
}

func NewAllowAndDenyIpHandler(
    handler *Handler,
    allowAndDenyIpService service.AllowAndDenyIpService,
) *AllowAndDenyIpHandler {
	return &AllowAndDenyIpHandler{
		Handler:      handler,
		allowAndDenyIpService: allowAndDenyIpService,
	}
}

func (h *AllowAndDenyIpHandler) GetAllowAndDenyIp(ctx *gin.Context) {
	var req v1.AllowAndDenyIpRequest
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	res, err := h.allowAndDenyIpService.GetAllowAndDenyIp(ctx, int64(req.Id))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *AllowAndDenyIpHandler) GetAllowAndDenyIpList(ctx *gin.Context) {
	var req v1.AllowAndDenyIpRequest
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	res, err := h.allowAndDenyIpService.GetAllowAndDenyIpsAllByHostId(ctx, int64(req.HostId))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *AllowAndDenyIpHandler) AddAllowAndDenyIp(ctx *gin.Context) {
	var req v1.AllowAndDenyIpRequest
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	err := h.allowAndDenyIpService.AddAllowAndDenyIps(ctx,req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)
	
}

func (h *AllowAndDenyIpHandler) EditAllowAndDenyIp(ctx *gin.Context) {
	var req v1.AllowAndDenyIpRequest
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	err := h.allowAndDenyIpService.EditAllowAndDenyIps(ctx,req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *AllowAndDenyIpHandler) DeleteAllowAndDenyIp(ctx *gin.Context) {
	var req v1.DelAllowAndDenyIpRequest
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	err := h.allowAndDenyIpService.DeleteAllowAndDenyIps(ctx,req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)
}