package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	v1 "projectName/api/v1"
	"projectName/internal/service"
	"projectName/pkg/helper/resp"
)

type IpHandler struct {
	*Handler
	ipService service.IpService
}

func NewIpHandler(
	handler *Handler,
	ipService service.IpService,
) *IpHandler {
	return &IpHandler{
		Handler:   handler,
		ipService: ipService,
	}
}

func (h *IpHandler) AddIp(ctx *gin.Context) {
	var req v1.Ip
	if err := ctx.ShouldBind(&req); err != nil {
		resp.HandleError(ctx, http.StatusBadRequest, 1, err.Error(), nil)
		return
	}

	if err := h.ipService.AddIp(ctx, req.Ip); err != nil {
		resp.HandleError(ctx, http.StatusInternalServerError, 1, err.Error(), nil)
		return
	}
	resp.HandleSuccess(ctx, nil)
}

func (h *IpHandler) DeleteIp(ctx *gin.Context) {
	var req v1.Ip
	if err := ctx.ShouldBind(&req); err != nil {
		resp.HandleError(ctx, http.StatusBadRequest, 1, err.Error(), nil)
		return
	}

	if err := h.ipService.DeleteIp(ctx, req.Ip); err != nil {
		resp.HandleError(ctx, http.StatusInternalServerError, 1, err.Error(), nil)
		return
	}
	resp.HandleSuccess(ctx, nil)

}
