package compute

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

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

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

	// Name of the rescue disk
	// Required: true
	Name string `url:"name" json:"name" validate:"required"`

	// Boot type of image BIOS or UEFI
	// Required: true
	BootType string `url:"boottype" json:"boottype" validate:"imageBootType"`

	// Image type linux, windows or other
	// Required: true
	ImageType string `url:"imagetype" json:"imagetype" validate:"imageType"`

	// Storage policy id of disk. The rules of the specified storage policy will be used.
	// Required: true
	StoragePolicyID uint64 `url:"storage_policy_id" json:"storage_policy_id" validate:"required"`

	// Username for the image
	// Required: false
	Username string `url:"username,omitempty" json:"username,omitempty"`

	// Password for the image
	// Required: false
	Password string `url:"password,omitempty" json:"password,omitempty"`

	// Account ID to make the image exclusive
	// Required: false
	AccountID uint64 `url:"accountId,omitempty" json:"accountId,omitempty"`

	// Pool for image create
	// Required: false
	PoolName string `url:"poolName,omitempty" json:"poolName,omitempty"`

	// Does this machine supports hot resize
	// Default: false
	// Required: false
	HotResize bool `url:"hotresize" json:"hotresize"`
}

type wrapperCreateTemplateFromBlankRequest struct {
	CreateTemplateFromBlankRequest
	AsyncMode bool `url:"asyncMode"`
}

// CreateTemplateFromBlank creates template from boot disk of current compute in sync mode.
// It returns id of created compute and error.
func (c Compute) CreateTemplateFromBlank(ctx context.Context, req CreateTemplateFromBlankRequest) (uint64, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return 0, validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperCreateTemplateFromBlankRequest{
		CreateTemplateFromBlankRequest: req,
		AsyncMode:                      false,
	}

	url := "/cloudapi/compute/createTemplateFromBlank"

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

	result, err := strconv.ParseUint(string(res), 10, 64)
	if err != nil {
		return 0, err
	}

	return result, nil
}

// CreateTemplateFromBlankAsync creates template from boot disk of current compute in async mode.
// It returns guid of task and error.
func (c Compute) CreateTemplateFromBlankAsync(ctx context.Context, req CreateTemplateFromBlankRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperCreateTemplateFromBlankRequest{
		CreateTemplateFromBlankRequest: req,
		AsyncMode:                      true,
	}

	url := "/cloudapi/compute/createTemplateFromBlank"

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

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

	return result, nil
}
