package routers

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

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

// ListRequest struct to get a list of routers
type ListRequest struct {
	// Filter by display name
	// Required: false
	DisplayName string `url:"display_name,omitempty" json:"display_name,omitempty"`

	// Filter by enabled status
	// Required: false
	Enabled interface{} `url:"enabled,omitempty" json:"enabled,omitempty" validate:"omitempty,isBool"`

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

	// Filter by description
	// Required: false
	Description string `url:"description,omitempty" json:"description,omitempty"`

	// Filter by create date from
	// Required: false
	CreatedFrom string `url:"created_from,omitempty" json:"created_from,omitempty"`

	// Filter by create date to
	// Required: false
	CreatedTo string `url:"created_to,omitempty" json:"created_to,omitempty"`

	// Filter by update date from
	// Required: false
	UpdatedFrom string `url:"updated_from,omitempty" json:"updated_from,omitempty"`

	// Filter by update date to
	// Required: false
	UpdatedTo string `url:"updated_to,omitempty" json:"updated_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 (display_name, subnet, created_at, updated_at)
	// Required: false
	SortBy string `url:"sort_by,omitempty" json:"sort_by,omitempty" validate:"omitempty,oneof=display_name subnet created_at updated_at"`

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

// List of routers
func (i Routers) List(ctx context.Context, req ListRequest) (RoutersList, error) {
	res, err := i.ListRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	pools := []RoutersModel{}

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

	return pools, nil
}

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

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

	url := "/sdn/router/list"

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