package flips

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

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

// UpdateRequest struct to update a floating ip
type UpdateRequest struct {
	// ID of a floating IP
	// Required: true
	FloatingIPID string `url:"floating_ip_id" json:"floating_ip_id" validate:"required"`

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

	// ID of an external network port
	// Required: true
	ExtNetPortID string `url:"external_network_port_id" json:"external_network_port_id" validate:"required"`

	// ID of a logical network port
	// Required: true
	LogicalPortID string `url:"logical_port_id" json:"logical_port_id" validate:"required"`

	// ID of a router
	// Required: true
	RouterID string `url:"router_id" json:"router_id" validate:"required"`
}

// Update updates a floating ip
func (fi FloatingIPs) Update(ctx context.Context, req UpdateRequest) (*RecordFloatingIP, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/floating_ip/update"

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

	info := RecordFloatingIP{}

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

	return &info, nil
}
