package compute

import (
	"context"
	"net/http"

	"repository.basistech.ru/BASIS/dynamix-golang-sdk/v13/internal/constants"
	"repository.basistech.ru/BASIS/dynamix-golang-sdk/v13/internal/validators"
)

// CloneRequest struct to clone compute instance
type CloneRequest struct {
	// ID of compute instance
	// Required: true
	ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`

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

	// ID of the Storage Policy
	// Required: false
	StoragePolicyID uint64 `url:"storage_policy_id" json:"storage_policy_id"`

	// Timestamp of the parent's snapshot to create clone from
	// Required: false
	SnapshotTimestamp uint64 `url:"snapshotTimestamp" json:"snapshotTimestamp"`

	// Name of the parent's snapshot to create clone from
	// Required: false
	SnapshotName string `url:"snapshotName" json:"snapshotName"`

	// true ignore that the compute is running
	// Default: false
	// Required: false
	Force bool `url:"force" json:"force"`

	// The name of the pool to migrate disks to
	// Required: false
	PoolName string `url:"pool_name" json:"pool_name"`

	// The ID of the SEP to migrate disks to
	// Required: false
	SEPID uint64 `url:"sep_id" json:"sep_id"`
}

// Clone clones compute instance
func (c Compute) Clone(ctx context.Context, req CloneRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/compute/clone"

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

	return string(res), nil
}
