package flips

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

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

// List of floating ips
type ListRequest struct {
	// Filter by access group ID
	// Required: false
	AccessGroupID string `url:"access_group_id,omitempty" json:"access_group_id,omitempty"`

	//Is the external network enabled
	// Required: false
	Enabled interface{} `url:"enabled,omitempty" json:"enabled,omitempty" validate:"omitempty,isBool"`

	// Filter by Pv4 of the associated external network port
	// Required: false
	ExternalNetworkPortIPv4 string `url:"external_network_port_ipv4,omitempty" json:"external_network_port_ipv4,omitempty"`

	// Filter by IP of the associated logical port binding
	// Required: false
	LogicalPortBindingIP string `url:"logical_port_binding_ip,omitempty" json:"logical_port_binding_ip,omitempty"`

	// Display name of the associated logical port
	// Required: false
	LogicalPortDisplayName string `url:"logical_port_display_name,omitempty" json:"logical_port_display_name,omitempty"`

	// Filter by display name of the associated external network
	// Required: false
	ExternalNetworkDisplayName string `url:"external_network_display_name,omitempty" json:"external_network_display_name,omitempty"`

	// Filter by display name of the associated router
	// Required: false
	RouterDisplayName string `url:"router_display_name,omitempty" json:"router_display_name,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 (enabled, created_at, updated_at, external_network_port_ipv4, logical_port_binding_ip, logical_port_display_name, external_network_display_name, router_display_name)
	// 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 floating ips
func (fi FloatingIPs) List(ctx context.Context, req ListRequest) (*FloatingIPsList, error) {
	res, err := fi.ListRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	objects := []RecordFloatingIP{}

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

	result := FloatingIPsList{Objects: objects}

	return &result, nil
}

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

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

	url := "/sdn/floating_ip/list"

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