package compute

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

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

// NetAttachRequest struct to attach network
type NetAttachRequest struct {
	// ID of compute instance
	// Required: true
	ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`

	// Network type
	// 'EXTNET' for connect to external network directly
	// 'VINS' for connect to ViNS
	// 'VFNIC' for connect to vfpool
	// 'DPDK' for connect to DPDK
	// `EMPTY` for connect empty network
	// `SDN` for connect to SDN
	// `TRUNK` for connect to TRUNK
	// Required: true
	NetType string `url:"netType" json:"netType" validate:"computex86NetType"`

	// Network ID for connect to
	// For EXTNET - external network ID
	// For VINS - VINS ID
	// Required: true
	NetID uint64 `url:"netId" json:"netId" validate:"required"`

	// Directly required IP address for new network interface
	// Required: false
	IPAddr string `url:"ipAddr,omitempty" json:"ipAddr,omitempty"`

	// MAC address
	// Required: false
	MACAddr string `url:"mac_addr,omitempty" json:"mac_addr,omitempty"`

	// Used for EXTNET, TRUNK and DPDK
	// Must be 1500-9216
	// Required: false
	MTU uint64 `url:"mtu,omitempty" json:"mtu,omitempty" validate:"omitempty,mtu"`

	// Net mask
	// Used only to DPDK or VFNIC net type
	// Required: false
	NetMask uint64 `url:"netMask,omitempty" json:"netMask,omitempty"`

	// SDN Segment ID
	// Required: false
	SDNSegmentID string `url:"sdn_segment_id,omitempty" json:"sdn_segment_id,omitempty"`

	// SDN Object Group ID
	// Required: false
	SDNObjectGroupID string `url:"sdn_object_group_id,omitempty" json:"sdn_object_group_id,omitempty"`

	// SDN Logical Port Display Name
	// Required: false
	SDNLogicalPortDisplayName string `url:"sdn_logical_port_display_name,omitempty" json:"sdn_logical_port_display_name,omitempty"`

	// SDN Logical Port Description
	// Required: false
	SDNLogicalPortDescription string `url:"sdn_logical_port_description,omitempty" json:"sdn_logical_port_description,omitempty"`

	// Unique identifier of logical port on SDN side
	// Required: false
	SDNInterfaceID string `url:"sdn_interface_id,omitempty" json:"sdn_interface_id,omitempty" validate:"omitempty"`

	// List of security group IDs to assign to this interface
	// Required: false
	SecGroups []uint64 `url:"security_groups,omitempty" json:"security_groups,omitempty"`

	// Flag indicating whether security groups are enabled for this interface
	// Not applicable to netType VFNIC, TRUNK, or SDN
	// Required: false
	EnableSecGroups bool `url:"enable_secgroups,omitempty" json:"enable_secgroups,omitempty"`

	// Flag indicating whether this interface is enabled (only for VINS, EXTNET, DPDK, SDN, TRUNK)
	// Required: false
	Enabled interface{} `url:"enabled,omitempty" json:"enabled,omitempty" validate:"omitempty,isBool"`
}

// NetAttach attaches network to compute and gets info about network
func (c Compute) NetAttach(ctx context.Context, req NetAttachRequest) (*RecordNetAttach, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/compute/netAttach"

	res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, req)
	if err != nil {
		return nil, err
	}

	info := RecordNetAttach{}

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

	return &info, nil
}
