package compute

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

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

// MassStopRequest struct to stop several computes
type MassStopRequest struct {
	// IDs of compute instances to stop
	// Required: true
	ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"min=1"`

	// Force stop compute
	// Required: false
	Force bool `url:"force,omitempty" json:"force,omitempty"`
}

type wrapperMassStopRequest struct {
	MassStopRequest

	AsyncMode bool `url:"asyncMode"`
}

// MassStop starts jobs to stop several computes
func (c Compute) MassStop(ctx context.Context, req MassStopRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperMassStopRequest{
		MassStopRequest: req,
		AsyncMode:       false,
	}

	url := "/cloudbroker/compute/massStop"

	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
}

// MassStopAsync starts jobs to stop several computes with AsyncMode
func (c Compute) MassStopAsync(ctx context.Context, req MassStopRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperMassStopRequest{
		MassStopRequest: req,
		AsyncMode:       true,
	}

	url := "/cloudbroker/compute/massStop"

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

	return string(res), nil
}
