package handlers

import (
	"net/http"
	"otaupdate/services"

	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/httphandlers"
	"github.com/fiskerinc/cloud-services/pkg/utils"
	"github.com/google/uuid"
	"github.com/fiskerinc/cloud-services/pkg/loggerdataresp"
)

// @deprecated
// HandleSubscriptionFeatureAssign godoc
// @Summary Assign feature to package
// @Description Assign feature to package
// @Accept  json
// @Produce  json
// @Param Authorization header string false "Bearer <ID token>"
// @Param Api-Key header string false "<API token>"
// @Param feature body SubFeatureAssignRequest true "Assignment package data"
// @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 /subscriptionpackagefeature [post]
func HandleSubscriptionFeatureAssign(w http.ResponseWriter, r *http.Request) {
	item := SubFeatureAssignRequest{}
	err := httphandlers.ParseRequest(r, &item)
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusBadRequest) {
		return
	}

	db := services.GetDB().GetSubPackages()
	_, err = db.AssociateFeature(item.PackageID, item.FeatureID)
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusServiceUnavailable, loggerdataresp.PostgresNoRowsErrorCheck) {
		return
	}

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

type SubFeatureAssignRequest struct {
	PackageID uuid.UUID `json:"package_id" validate:"required"`
	FeatureID uuid.UUID `json:"feature_id" validate:"required"`
}
