package compute

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

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

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

type wrapperDisableRequest struct {
	DisableRequest

	AsyncMode bool `url:"asyncMode"`
}

// Disable disables compute
func (c Compute) Disable(ctx context.Context, req DisableRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperDisableRequest{
		DisableRequest: req,
		AsyncMode:      false,
	}

	url := "/cloudbroker/compute/disable"

	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
}

// DisableAsync disables compute with AsyncMode
func (c Compute) DisableAsync(ctx context.Context, req DisableRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperDisableRequest{
		DisableRequest: req,
		AsyncMode:      true,
	}

	url := "/cloudbroker/compute/disable"

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

	return string(res), nil
}
