package hypervisors

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

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

// ListRequest struct to get a list of hypervisors
type ListRequest struct {
	// Page
	// Required: false
	Page uint64 `url:"page,omitempty" json:"page,omitempty"`

	// Per page
	// Required: false
	PerPage uint64 `url:"per_page,omitempty" json:"per_page,omitempty"`

	// Sort by (available options: name, hostname, last_sync, display_name, ip, created_at, updated_at)
	// Required: false
	SortBy string `url:"sort_by,omitempty" json:"sort_by,omitempty"`

	// Sort order (available options: asc, desc)
	// Required: false
	SortOrder string `url:"sort_order,omitempty" json:"sort_order,omitempty"`

	// Port info (available options: detailed, general)
	// Required: false
	PortInfo string `url:"port_info,omitempty" json:"port_info,omitempty"`

	// Hostname
	// Required: false
	Hostname string `url:"hostname,omitempty" json:"hostname,omitempty"`

	// Display name
	// Required: false
	DisplayName string `url:"display_name,omitempty" json:"display_name,omitempty"`

	// IP
	// Required: false
	IP string `url:"ip,omitempty" json:"ip,omitempty"`

	// Created from
	// Required: false
	CreatedFrom string `url:"created_from,omitempty" json:"created_from,omitempty"`

	// Created to
	// Required: false
	CreatedTo string `url:"created_to,omitempty" json:"created_to,omitempty"`

	// Updated from
	// Required: false
	UpdatedFrom string `url:"updated_from,omitempty" json:"updated_from,omitempty"`

	// Updated to
	// Required: false
	UpdatedTo string `url:"updated_to,omitempty" json:"updated_to,omitempty"`
}

// List of hypervisors
func (hv Hypervisors) List(ctx context.Context, req ListRequest) (HypervisorsList, error) {
	res, err := hv.ListRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	hvs := HypervisorsList{}

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

	return hvs, nil
}

// ListRaw gets a list of all hypervisors as an array of bytes
func (hv Hypervisors) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {

	if err := validators.ValidateRequest(req); err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/hypervisor/list"

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