package api

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/pingcap/dbaas/admin/forms"
	"github.com/pingcap/dbaas/central/model/apikey"
)

// @Summary Add API Key
// @tags ApiKey
// @Accept  json
// @Produce json
// @Param user body forms.CreateAPIKey true "Add API key"
// @Success 200 {object} forms.CreateAPIKeyResp
// @Router /api_key [post]
func CreateAPIKey(c *gin.Context) {
	req := forms.CreateAPIKey{}
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err, http.StatusBadRequest)
		return
	}

	key, err := apikey.CreateAPIKey(req.TenantID, req.CreatorUserId, req.KeyName)
	if err != nil {
		Fail(c, err, http.StatusInternalServerError)
		return
	}

	Success(c, buildAPIKey(key), http.StatusOK)
}
