package extnet

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

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

// GetRequest struct to get information about reserved address or address poll
type GetReservedIP struct {
	// AccountID of the account whose reservation information we want to receive
	// Required: true
	AccountID uint64 `url:"accountId" json:"accountId" validate:"required"`

	// Field for specifying the ID of extnet whose reservation information we want to receive
	// Required: false
	ExtNetID uint64 `url:"extnetId,omitempty" json:"extnetId,omitempty"`
}

// GetReservedIP gets information about reserved address or address poll as a slice of RecordReservedIP struct
func (e ExtNet) GetReservedIP(ctx context.Context, req GetReservedIP) ([]RecordReservedIP, error) {
	res, err := e.GetReservedIPRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	reservedIP := make([]RecordReservedIP, 0)

	err = json.Unmarshal(res, &reservedIP)
	if err != nil {
		return nil, err
	}

	return reservedIP, nil
}

// GetRaw gets detailed information about external network as an array of bytes
func (e ExtNet) GetReservedIPRaw(ctx context.Context, req GetReservedIP) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/extnet/getReservedIp"

	res, err := e.client.DecortApiCall(ctx, http.MethodPost, url, req)
	return res, err
}
