package extnet

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

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

// PortUpdateRequest struct for update port to extnet
type PortUpdateRequest struct {
	// ID of external network
	// Required: true
	ExtNetID string `url:"external_network_id" json:"external_network_id" validate:"required"`

	// Access group ID
	// Required: true
	AccessGroupID string `url:"access_group_id" json:"access_group_id" validate:"required"`

	// Port ID
	// Required: true
	PortID string `url:"port_id" json:"port_id" validate:"required"`

	// Port version ID
	// Required: true
	PortVersionID uint64 `url:"port_version_id" json:"port_version_id" validate:"required"`

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

	// Description of the port addition operation
	// Required: true
	Comment string `url:"comment" json:"comment" validate:"required"`

	// User-friendly name for the external network
	// Required: true
	DisplayName string `url:"display_name" json:"display_name" validate:"required"`

	// Whether the network is enabled
	// Required: true
	Enabled bool `url:"enabled" json:"enabled"`

	// IPv4
	// Required: false
	IPv4 string `url:"ipv4,omitempty" json:"ipv4,omitempty"`

	// IPv6
	// Required: false
	IPv6 string `url:"ipv6,omitempty" json:"ipv6,omitempty"`

	// IPv6 Config
	// Required: false
	IPv6Config *IPv6ConfigRequest `url:"-" json:"ipv6_config,omitempty"`

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

// UpdatePort updated a port an external network
func (e ExtNet) UpdatePort(ctx context.Context, req PortUpdateRequest) (*ExternalNetworkResponse, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/external_network/port_update"

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

	info := ExternalNetworkResponse{}

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

	return &info, nil
}
