package logicalports

import (
	"context"
	"net/http"

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

type CreateBatchLogicalPortAddress struct {
	// IP
	// Required: true
	IP string `url:"ip" json:"ip" validate:"required"`

	// IP Type
	// Required: true
	IPType string `url:"ip_type" json:"ip_type" validate:"required"`

	// Is Discovered
	// Required: true
	IsDiscovered bool `url:"is_discovered" json:"is_discovered" validate:"required"`

	// Is Primary
	// Required: true
	IsPrimary bool `url:"is_primary" json:"is_primary" validate:"required"`

	// MAC
	// Required: false
	MAC string `url:"mac,omitempty" json:"mac,omitempty"`
}

type CreateBatchBindings struct {
	// Address Detection
	// Required: true
	AddressDetection bool `url:"address_detection" json:"address_detection" validate:"required"`

	// Logical Port Address
	// Required: true
	LogicalPortAddresses []CreateBatchLogicalPortAddress `url:"logical_port_addresses" json:"logical_port_addresses" validate:"required,dive"`

	// Port Security
	// Required: true
	PortSecurity bool `url:"port_security" json:"port_security" validate:"required"`

	// Segment ID
	// Required: true
	SegmentID string `url:"segment_id" json:"segment_id" validate:"required"`
}

type CreateBatchLabels struct {
	// VM ID
	// Required: false
	VMID string `url:"vm_id,omitempty" json:"vm_id,omitempty"`

	// VM Name
	// Required: false
	VMName string `url:"vm_name,omitempty" json:"vm_name,omitempty"`
}

type NetObjectGroups struct {
	// ID
	// Required: true
	ID string `url:"id" json:"id" validate:"required"`

	// Version ID
	// Required: true
	VersionID uint64 `url:"version_id" json:"version_id" validate:"required"`
}

type Ports struct {
	// Adapter MAC
	// Required: false
	AdapterMAC string `url:"adapter_mac,omitempty" json:"adapter_mac,omitempty"`

	// Bindings
	// Required: true
	Bindings CreateBatchBindings `url:"bindings" json:"bindings" validate:"required"`

	// Description
	// Required: true
	Description string `url:"description" json:"description" validate:"required"`

	// Display Name
	// Required: true
	DisplayName string `url:"display_name" json:"display_name" validate:"required"`

	// Enabled
	// Required: true
	Enabled bool `url:"enabled" json:"enabled" validate:"required"`

	// Hypervisor
	// Required: true
	Hypervisor string `url:"hypervisor" json:"hypervisor" validate:"required"`

	// Net Object Groups
	// Required: false
	NetObjectGroups []NetObjectGroups `json:"net_object_groups" validate:"required,dive"`

	// Labels
	// Required: false
	Labels []CreateBatchLabels `url:"labels,omitempty" json:"labels,omitempty" validate:"required,dive"`

	// Unique Identifier
	// Required: false
	UniqueIdentifier string `url:"unique_identifier,omitempty" json:"unique_identifier,omitempty"`
}

// CreateBatchRequest struct to create a batch of logical ports
type CreateBatchRequest struct {
	// Access Group ID
	// Required: true
	AccessGroupID string `url:"access_group_id" json:"access_group_id" validate:"required"`

	// Ports
	// Required: true
	Ports []Ports `json:"ports" validate:"required,dive"`
}

// CreateBatch creates a batch of logical ports
func (lp LogicalPorts) CreateBatch(ctx context.Context, req CreateBatchRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}
	url := "/sdn/logical_port/create_batch"

	_, err = lp.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, req)
	if err != nil {
		return false, err
	}

	return true, nil
}
