package extnet

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

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

// AddReserveIPRequest struct to reserved address or address poll
type AddReserveIPRequest struct {
	// AccountID to which a reserved address or address pool is added
	// Required: true
	AccountID uint64 `url:"accountId" json:"accountId" validate:"required"`

	// ExtNetID from which the address or address pool is reserved
	// Required: true
	ExtNetID uint64 `url:"extnetId" json:"extnetId" validate:"required"`

	// Field for specifying the number of reserved addresses
	// Required: false
	IPCount uint64 `url:"ipCount,omitempty" json:"ipCount,omitempty"`

	// List of IPs for specifying the desired address
	// Required: false
	IPs []string `url:"ips,omitempty" json:"ips,omitempty"`

	// Flag indicating whether the reserved IP can be auto-assigned
	// Required: false
	// Default: true
	AutoAssignable interface{} `url:"auto_assignable,omitempty" json:"auto_assignable,omitempty" validate:"omitempty,isBool"`
}

// AddReserveIP reserves address or address poll to external network for account ID
func (e ExtNet) AddReserveIP(ctx context.Context, req AddReserveIPRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/extnet/addReservedIp"

	res, err := e.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
}
