package adrspools

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

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

// ListAddressPoolsRequest struct to get a list of a groups
type ListAddressPoolsRequest struct {
	// Filter by access group ID
	// Required: false
	AccessGroupID string `url:"access_group_id,omitempty" json:"access_group_id,omitempty"`

	// Filter by name
	// Required: false
	Name string `url:"name,omitempty" json:"name,omitempty"`

	// Filter by minimum address count (greater than or equal to)
	// Required: false
	AddrNumberMin uint64 `url:"addr_number_min,omitempty" json:"addr_number_min,omitempty"`

	// Filter by maximum address count (less than)
	// Required: false
	AddrNumberMax uint64 `url:"addr_number_max,omitempty" json:"addr_number_max,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, addr_count, 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"`
}

// List of address pools
func (i AddressPools) List(ctx context.Context, req ListAddressPoolsRequest) (*AddressPoolsList, error) {
	res, err := i.ListRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	pools := []NetworkAddressPool{}

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

	result := AddressPoolsList{Pools: pools}

	return &result, nil
}

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

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

	url := "/sdn/access_group/list"

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