package compute

import (
	"context"
	"net/http"
	"strings"

	"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)

// CreateTemplateRequest struct to create template
type CreateTemplateRequest struct {
	// ID of the compute to create template from
	// Required: true
	ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`

	// Name to assign to the template being created
	// Required: true
	Name string `url:"name" json:"name" validate:"required"`
}

type wrapperCreateTemplateRequest struct {
	CreateTemplateRequest

	AsyncMode bool `url:"asyncMode"`
}

// CreateTemplate create template from compute instance
func (c Compute) CreateTemplate(ctx context.Context, req CreateTemplateRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/compute/createTemplate"

	res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, req)
	if err != nil {
		return "", err
	}

	result := strings.ReplaceAll(string(res), "\"", "")

	return result, nil
}
