package handlers

import (
	"net/http"

	"otaupdate/controllers"
	"otaupdate/services"

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

	"github.com/go-pg/pg/v10/orm"
	"github.com/gorilla/schema"
)

// @deprecated
// SubscriptionPackageDelete godoc
// @Summary Delete subscription package
// @Description Delete subscription package data. Requires delete permissions
// @Accept  json
// @Produce  json
// @Param Authorization header string false "Bearer <ID token>"
// @Param Api-Key header string false "<API token>"
// @Param id query string true "Subscription package id"
// @Success 200 {object} common.JSONMessage
// @Failure 400 {object} common.JSONError "Bad request"
// @Failure 401 {object} common.JSONError "Unauthorized"
// @Failure 503 {object} common.JSONError "Service unavailable"
// @Router /subscriptionpackage [delete]
func HandleSubscriptionPackageDelete(w http.ResponseWriter, r *http.Request) {
	subscriptionPackageDelete.Handle(w, r)
}

var subscriptionPackageDelete = controllers.NewDelete(&subscriptionPackageDeleteHelper{})

type subscriptionPackageDeleteHelper struct {
	SubscriptionPackagesHelper
}

func (h *subscriptionPackageDeleteHelper) ParseDeleteQueryParams(r *http.Request) interface{} {
	req := common.SubscriptionPackage{}
	decoder := schema.NewDecoder()

	decoder.SetAliasTag("json")
	decoder.Decode(&req, r.URL.Query())

	return &req
}

func (h *subscriptionPackageDeleteHelper) QueryDelete(model interface{}) (orm.Result, error) {
	return services.GetDB().GetSubPackages().Delete(model.(*common.SubscriptionPackage))
}
