package handlers

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

	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/go-pg/pg/v10/orm"
)

// HandleAPITokenAdd godoc
// @Summary Add API token
// @Description Create 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.APIToken
// @Failure 400 {object} common.JSONError "Bad request"
// @Failure 401 {object} common.JSONError "Unauthorized"
// @Failure 503 {object} common.JSONError "Service unavailable"
// @Router /apitoken [post]
func HandleAPITokenAdd(w http.ResponseWriter, r *http.Request) {
	apiTokenAdd.Handle(w, r)
}

var apiTokenAdd = controllers.NewCreate(&apiTokenCreateHelper{})

type apiTokenCreateHelper struct {
	APITokensHelper
}

func (h *apiTokenCreateHelper) QueryInsert(model interface{}) (orm.Result, error) {
	return services.GetDB().GetAPITokens().Insert(*model.(*common.APIToken))
}
