package logicalports

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

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

// Label struct
type UpdateLabels struct {
	VMID   string `url:"vm_id,omitempty" json:"vm_id,omitempty"`
	VMName string `url:"vm_name,omitempty" json:"vm_name,omitempty"`
}

// UpdateRequest struct to update logical port
type UpdateRequest struct {
	// ID of the logical port
	// Required: true
	LogicalPortID string `url:"logical_port_id" json:"logical_port_id" validate:"required"`

	// ID of the version
	// Required: true
	VersionID uint64 `url:"version_id" json:"version_id" validate:"required"`

	// Adapter MAC
	// Required: true
	AdapterMAC string `url:"adapter_mac" json:"adapter_mac" 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. True or False
	// Required: true
	Enabled interface{} `url:"enabled" json:"enabled" validate:"required,isBool"`

	// Remove addresses
	// Required: false
	RemoveAddresses []string `url:"remove_addresses,omitempty" json:"remove_addresses,omitempty"`

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

	// Port security. True or False
	// Required: true
	PortSecurity interface{} `url:"port_security" json:"port_security" validate:"required,isBool"`

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

	// Update addresses
	// Required: false
	UpdateAddresses []UpdateAddress `url:"update_addresses,omitempty" json:"update_addresses,omitempty" validate:"dive"`

	// Add addresses
	// Required: false
	AddAddresses []AddAddress `url:"add_addresses,omitempty" json:"add_addresses,omitempty" validate:"dive"`

	// Labels
	// Required: false
	Labels *UpdateLabels `url:"labels,omitempty" json:"labels"`
}

// UpdateAddress struct representing update address
type UpdateAddress struct {
	// ID of the address
	// Required: true
	ID string `url:"id" json:"id" validate:"required"`

	// IP address
	// Required: true
	IP string `url:"ip" json:"ip" validate:"required"`

	// IP type
	// Required: true
	IPType string `url:"ip_type" json:"ip_type" validate:"required,oneof=IPv4 IPv6"`

	// Is discovered. True or False
	// Required: true
	IsDiscovered interface{} `url:"is_discovered" json:"is_discovered" validate:"required,isBool"`

	// Is primary. True or False
	// Required: true
	IsPrimary interface{} `url:"is_primary" json:"is_primary" validate:"required,isBool"`

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

// AddAddress struct representing add address
type AddAddress struct {
	// IP address
	// Required: true
	IP string `url:"ip" json:"ip" validate:"required"`

	// IP type
	// Required: true
	IPType string `url:"ip_type" json:"ip_type" validate:"required,oneof=IPv4 IPv6"`

	// Is discovered
	// Required: true
	IsDiscovered interface{} `url:"is_discovered" json:"is_discovered" validate:"required,isBool"`

	// Is primary
	// Required: true
	IsPrimary interface{} `url:"is_primary" json:"is_primary" validate:"required,isBool"`

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

// Update updates a logical port
func (i LogicalPorts) Update(ctx context.Context, req UpdateRequest) (*LogicalPort, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/logical_port/update"

	res, err := i.client.DecortApiCallCtype(ctx, http.MethodPut, url, constants.MIMEJSON, req)
	if err != nil {
		return nil, err
	}

	info := LogicalPort{}

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

	return &info, nil
}
