package qospolicy

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

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

type ListRequest struct {
	// Page number
	// Required: false
	Page uint64 `url:"page,omitempty" json:"page,omitempty"`

	// Page size
	// Required: false
	Size uint64 `url:"size,omitempty" json:"size,omitempty"`

	// Find QoS policy by id
	// Required: false
	ByID uint64 `url:"by_id,omitempty" json:"by_id,omitempty"`

	// Find by account id
	// Required: false
	AccountID uint64 `url:"account_id,omitempty" json:"account_id,omitempty"`

	// Find by QoS policy name
	// Required: false
	Name string `url:"name,omitempty" json:"name,omitempty"`

	// Find by QoS policy description
	// Required: false
	Description string `url:"description,omitempty" json:"description,omitempty"`

	// Find by QoS policy status
	// Possible values: ENABLED, DISABLED, DESTROYED
	// Required: false
	Status string `url:"status,omitempty" json:"status,omitempty"`

	// Find by created after time (unix timestamp)
	// Required: false
	CreatedMin uint64 `url:"created_min,omitempty" json:"created_min,omitempty"`

	// Find by created before time (unix timestamp)
	// Required: false
	CreatedMax uint64 `url:"created_max,omitempty" json:"created_max,omitempty"`

	// Find by updated after time (unix timestamp)
	// Required: false
	UpdatedMin uint64 `url:"updated_min,omitempty" json:"updated_min,omitempty"`

	// Find by updated before time (unix timestamp)
	// Required: false
	UpdatedMax uint64 `url:"updated_max,omitempty" json:"updated_max,omitempty"`

	// Sort by one of supported fields, format ±<field>
	// Required: false
	SortBy string `url:"sort_by,omitempty" json:"sort_by,omitempty"`
}

// List gets list of QoS policies as a ListQosPolicies struct
func (qp QoSPolicy) List(ctx context.Context, req ListRequest) (*ListQoSPolicies, error) {
	res, err := qp.ListRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	list := ListQoSPolicies{}

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

	return &list, nil
}

// ListRaw gets list of QoS policies as an array of bytes
func (qp QoSPolicy) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {
	if err := validators.ValidateRequest(req); err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/network_qos_policy/list"

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