package logicalports

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

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

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

	// Find by segment ID
	// Required: false
	SegmentID string `url:"segment_id,omitempty" json:"segment_id,omitempty"`

	// Find by segment display name
	// Required: false
	SegmentDisplayName string `url:"segment_display_name,omitempty" json:"segment_display_name,omitempty"`

	// Find by external network ID
	// Required: false
	ExternalNetworkID string `url:"external_network_id,omitempty" json:"external_network_id,omitempty"`

	// Find by unique identifier
	// Required: false
	UniqueIdentifier string `url:"unique_identifier,omitempty" json:"unique_identifier,omitempty"`

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

	// Find by adapter MAC address
	// Required: false
	AdapterMAC string `url:"adapter_mac,omitempty" json:"adapter_mac,omitempty"`

	// Find by hypervisor
	// Required: false
	Hypervisor string `url:"hypervisor,omitempty" json:"hypervisor,omitempty"`

	// Find by hypervisor display name
	// Required: false
	HypervisorDisplayName string `url:"hypervisor_display_name,omitempty" json:"hypervisor_display_name,omitempty"`

	// Find by live migration target hypervisor
	// Required: false
	LiveMigrationTargetHv string `url:"live_migration_target_hv,omitempty" json:"live_migration_target_hv,omitempty"`

	// Find by port security status, true or false
	// Required: false
	PortSecurity interface{} `url:"port_security,omitempty" json:"port_security,omitempty" validate:"omitempty,isBool"`

	// Find by address detection status, true or false
	// Required: false
	AddressDetection interface{} `url:"address_detection,omitempty" json:"address_detection,omitempty" validate:"omitempty,isBool"`

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

	// Creation date lower bound (inclusive)
	// Required: false
	CreatedFrom string `url:"created_from,omitempty" json:"created_from,omitempty"`

	// Creation date upper bound (inclusive)
	// 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, segment_id, hypervisor, port_security, segment_display_name, primary_address, hypervisor_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 logical ports
func (i LogicalPorts) List(ctx context.Context, req ListRequest) (*LogicalPortsList, error) {
	res, err := i.ListRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	groups := []LogicalPort{}

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

	result := LogicalPortsList{Ports: groups}

	return &result, nil
}

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

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

	url := "/sdn/logical_port/list"

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