package netobjgroups

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

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

// ListRequest struct to get a list of network object groups
type ListRequest struct {
	// Filter by name
	// Required: false
	Name string `url:"name,omitempty" json:"name,omitempty"`

	// Filter by access group ID
	// Required: false
	AccessGroupID string `url:"access_group_id,omitempty" json:"access_group_id,omitempty"`

	// Updated at lower bound (greater than or equal to)
	// Required: false
	UpdatedFrom string `url:"updated_from,omitempty" json:"updated_from,omitempty"`

	// Updated at upper bound (less than)
	// Required: false
	UpdatedTo string `url:"updated_to,omitempty" json:"updated_to,omitempty"`

	// Created at lower bound (greater than or equal to)
	// Required: false
	CreatedFrom string `url:"created_from,omitempty" json:"created_from,omitempty"`

	// Created at upper bound (less than)
	// Required: false
	CreatedTo string `url:"created_to,omitempty" json:"created_to,omitempty"`

	// Page number for pagination
	// Required: false
	Page uint64 `url:"page,omitempty" json:"page,omitempty"`

	// Number of results per page
	// Required: false
	PerPage uint64 `url:"per_page,omitempty" json:"per_page,omitempty"`

	// Field to sort by (name, created_at, updated_at)
	// Required: false
	SortBy string `url:"sort_by,omitempty" json:"sort_by,omitempty"`

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

	// Filter by type
	// Required: false
	Type string `url:"type,omitempty" json:"type,omitempty"`
}

// List of address pools
func (nog NetworkObjectGroups) List(ctx context.Context, req ListRequest) (*NetObjGroupList, error) {
	res, err := nog.ListRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	objects := []RecordNetObjGroup{}

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

	result := NetObjGroupList{Objects: objects}

	return &result, nil
}

// ListRaw gets a list of all address pools as an array of bytes
func (nog NetworkObjectGroups) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {

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

	url := "/sdn/network_object_group/list"

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