package routers

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 for update router
type UpdateRequest struct {
	// ID of router
	// Required: true
	RouterID string `url:"router_id" json:"router_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"`

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

// Update updated a router
func (e Routers) Update(ctx context.Context, req UpdateRequest) (*RoutersModel, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/router/update"

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

	info := RoutersModel{}

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

	return &info, nil
}
