package logicalports

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

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

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

// CreateRequest struct to create logical port
type CreateRequest struct {
	// ID of the access group
	// Required: true
	AccessGroupID string `url:"access_group_id" json:"access_group_id" 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"`

	// 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"`

	// Adapter MAC
	// Required: false
	AdapterMAC string `url:"adapter_mac,omitempty" json:"adapter_mac,omitempty"`

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

	// Logical port addresses
	// Required: false
	LogicalPortAddresses []LogicalPortAddressRequest `url:"logical_port_addresses,omitempty" json:"logical_port_addresses,omitempty" validate:"dive"`

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

// LogicalPortAddressRequest struct representing logical port address
type LogicalPortAddressRequest struct {
	// IP address. IPv4 or IPv6 address of the logical port
	// If nil (allowed only when IsPrimary is true), the system will automatically generate a valid address
	// Required: true
	IP interface{} `url:"ip" json:"ip" validate:"omitempty,isString"`

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

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

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

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

// Create creates a logical port
func (l LogicalPorts) Create(ctx context.Context, req CreateRequest) (*LogicalPort, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/logical_port/create"

	res, err := l.client.DecortApiCallCtype(ctx, http.MethodPost, 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
}
