package compute

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

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

// MigrateStorageAbortRequest struct to abort migration
type MigrateStorageAbortRequest struct {
	// ID of the compute instance
	// Required: true
	ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`

	// Status check
	// Default: false
	// Required: false
	StatusCheck bool `url:"statusCheck" json:"statusCheck"`
}

type wrapperMigrateStorageAbortRequest struct {
	MigrateStorageAbortRequest

	AsyncMode bool `url:"asyncMode"`
}

// MigrateStorageAbort aborts complex compute migration job
func (c Compute) MigrateStorageAbort(ctx context.Context, req MigrateStorageAbortRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperMigrateStorageAbortRequest{
		MigrateStorageAbortRequest: req,
		AsyncMode:                  false,
	}

	url := "/cloudbroker/compute/migrateStorageAbort"

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

	result, err := strconv.ParseBool(string(res))
	if err != nil {
		return false, err
	}

	return result, nil
}

// MigrateStorageAbortAsync aborts complex compute migration job with AsyncMode
func (c Compute) MigrateStorageAbortAsync(ctx context.Context, req MigrateStorageAbortRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperMigrateStorageAbortRequest{
		MigrateStorageAbortRequest: req,
		AsyncMode:                  true,
	}

	url := "/cloudbroker/compute/migrateStorageAbort"

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

	return string(res), nil
}
