package handlers

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

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

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

// APITokenUpdate godoc
// @Summary Update API token
// @Description Update API token. Requires API token permission
// @Accept  json
// @Produce  json
// @Param Authorization header string false "Bearer <ID token>"
// @Param Api-Key header string false "<API token>"
// @Param data body common.APIToken true "API token data"
// @Success 200 {object} common.SubscriptionConfiguration
// @Failure 400 {object} common.JSONError "Bad request"
// @Failure 401 {object} common.JSONError "Unauthorized"
// @Failure 503 {object} common.JSONError "Service unavailable"
// @Router /apitoken [put]
func HandleAPITokenUpdate(w http.ResponseWriter, r *http.Request) {
	apiTokenUpdate.Handle(w, r)
}

var apiTokenUpdate = controllers.NewUpdate(&apiTokenUpdateHelper{})

type apiTokenUpdateHelper struct {
	APITokensHelper
}

func (h *apiTokenUpdateHelper) QueryUpdate(model interface{}) (orm.Result, error) {
	return services.GetDB().GetAPITokens().Update(model.(*common.APIToken))
}
