package compute

import (
	"context"
	"encoding/json"
	"net/http"

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

// CheckComputePlacementRequest struct for check compute placement
type CheckComputePlacementRequest struct {
	// IDs of compute instances to check
	// Required: true
	ComputeIDs []uint64 `url:"compute_ids" json:"compute_ids" validate:"required"`

	// Filter by CPU and RAM when checking placement
	// Required: false
	// Default: true
	FilterByCPURAM interface{} `url:"filter_by_cpu_ram,omitempty" json:"filter_by_cpu_ram,omitempty" validate:"omitempty,isBool"`
}

// CheckComputePlacement checks compute placement and returns structured result
func (c Compute) CheckComputePlacement(ctx context.Context, req CheckComputePlacementRequest) (CheckComputePlacementResult, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/compute/check_compute_placement"

	if req.FilterByCPURAM == nil {
		req.FilterByCPURAM = true
	}

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

	result := make(CheckComputePlacementResult)
	if err := json.Unmarshal(res, &result); err != nil {
		return nil, err
	}

	return result, nil
}
