package extnet

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

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

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

	// Filter by display name
	// Required: false
	DisplayName string `url:"display_name,omitempty" json:"display_name,omitempty"`

	// Filter by IP version (v4 or v6)
	// Required: false
	Subnet string `url:"subnet,omitempty" json:"subnet,omitempty" validate:"omitempty,ipTypes"`

	// Filter by IPv4 subnet (CIDR notation)
	// Required: false
	SubnetV4 string `url:"subnet_v4,omitempty" json:"subnet_v4,omitempty"`

	// Filter by IPv4 subnet (CIDR notation)
	// Required: false
	SubnetV6 string `url:"subnet_v6,omitempty" json:"subnet_v6,omitempty"`

	// Filter by exact bridge network name
	// Required: false
	BridgeNetworkName string `url:"bridge_network_name,omitempty" json:"bridge_network_name,omitempty"`

	// Filter by VLAN tag
	// Required: false
	VLANTag string `url:"vlan_tag,omitempty" json:"vlan_tag,omitempty"`

	// Filter by IPv4 default gateway
	// Required: false
	DefaultGatewayIPv4 string `url:"default_gateway_ipv4,omitempty" json:"default_gateway_ipv4,omitempty"`

	// Filter by IPv6 default gateway
	// Required: false
	DefaultGatewayIPv6 string `url:"default_gateway_ipv6,omitempty" json:"default_gateway_ipv6,omitempty"`

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

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

	// Filter lby update date to
	// Required: false
	UpdatedTo string `url:"updated_to,omitempty" json:"updated_to,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 operation status
	// Required: false
	OperationStatus string `url:"operation_status,omitempty" json:"operation_status,omitempty"`

	// Filter by hypervisor status
	// Required: false
	HypervisorStatus string `url:"hypervisor_status,omitempty" json:"hypervisor_status,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, created_at, updated_at, deleted_at, etc)
	// 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 gets list of all available external networks as a ListExtNet struct
func (e ExtNet) List(ctx context.Context, req ListRequest) (ListExtNet, error) {
	res, err := e.ListRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	list := ListExtNet{}

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

	return list, nil
}

// ListRaw gets list of all available external networks as an array of bytes
func (e ExtNet) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {

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

	url := "/sdn/external_network/list"

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