package controllers

import (
	"net/http"

	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/utils"
	"github.com/fiskerinc/cloud-services/pkg/loggerdataresp"

)

func NewMongoDelete(helper MongoDeleteHelperInterface) *MongoHandleDelete {
	return &MongoHandleDelete{Helper: helper}
}

type MongoDeleteHelperInterface interface {
	ParseDeleteURLParams(r *http.Request) interface{}
	ValidateFields(model interface{}) error
	QueryDelete(req interface{}) error
}

type MongoHandleDelete struct {
	Helper MongoDeleteHelperInterface
}

func (h *MongoHandleDelete) Handle(w http.ResponseWriter, r *http.Request) {
	filter := h.Helper.ParseDeleteURLParams(r)
	err := h.Helper.ValidateFields(filter)
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusBadRequest) {
		return
	}

	err = h.Helper.QueryDelete(filter)
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusNotFound) {
		return
	}

	utils.RespJSON(w, http.StatusOK, common.JSONMessage{
		Message: "Deleted",
	})
}
