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 WebForwardingHandler struct {
	*Handler
	webForwardingService service.WebForwardingService
}

func NewWebForwardingHandler(
	handler *Handler,
	webForwardingService service.WebForwardingService,
) *WebForwardingHandler {
	return &WebForwardingHandler{
		Handler:              handler,
		webForwardingService: webForwardingService,
	}
}

func (h *WebForwardingHandler) GetWebForwarding(ctx *gin.Context) {
	req := new(v1.GetForwardingRequest)

	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	res, err := h.webForwardingService.GetWebForwarding(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)
}

func (h *WebForwardingHandler) AddWebForwarding(ctx *gin.Context) {
	req := new(v1.WebForwardingRequest)

	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	 err := h.webForwardingService.AddWebForwarding(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *WebForwardingHandler) EditWebForwarding(ctx *gin.Context) {
	req := new(v1.WebForwardingRequest)

	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	 err := h.webForwardingService.EditWebForwarding(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *WebForwardingHandler) DeleteWebForwarding(ctx *gin.Context) {
	req := new(v1.DeleteWebForwardingRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	 err := h.webForwardingService.DeleteWebForwarding(ctx, req.Ids)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *WebForwardingHandler) GetWebForwardingList(ctx *gin.Context) {
	req := new(v1.GetForwardingRequest)
	if err := ctx.ShouldBind(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
		return
	}
	defaults.SetDefaults(req)
	res, err := h.webForwardingService.GetWebForwardingWafWebAllIps(ctx, *req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
		return
	}
	v1.HandleSuccess(ctx, res)

}