package compute

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

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

// ChangeReadOnlyRequest defines parameters for toggling read-only mode.
type ChangeReadOnlyRequest struct {
	// Compute ID
	// Required: true
	ComputeID uint64 `url:"compute_id" json:"compute_id" validate:"required"`

	// ReadOnly indicates whether the read-only mode is enabled
	// Required: true
	ReadOnly bool `url:"read_only" json:"read_only" validate:"required"`
}

// ChangeReadOnly toggles compute read-only mode.
func (c Compute) ChangeReadOnly(ctx context.Context, req ChangeReadOnlyRequest) (bool, error) {
	if err := validators.ValidateRequest(req); err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/compute/change_read_only"

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

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

	return result, nil
}
