package gwport

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"
)

// UpdateRequest struct to update gateway port
type UpdateRequest struct {
	// ID of router
	// Required: true
	RouterID string `url:"router_id" json:"router_id" validate:"required"`

	// ID of gateway port
	// Required: true
	GatewayPortID string `url:"gateway_port_id" json:"gateway_port_id" validate:"required"`

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

	// Detailed description of the external network
	// Required: true
	Description string `url:"description" json:"description" validate:"required"`

	// External network port ID
	// Required: true
	ExternalNetworkPortID string `url:"external_network_port_id" json:"external_network_port_id" validate:"required"`

	// Whether is enabled
	// Required: true
	Enabled bool `url:"snat_enabled" json:"snat_enabled"`
}

// Updated update a gateway port
func (i GWPort) Update(ctx context.Context, req UpdateRequest) (*GatewayPort, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/router/gateway_port/update"

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

	info := GatewayPort{}

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

	return &info, nil
}
