package node

import (
	"context"
	"net/http"
	"strconv"

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

// AddSSHIdentityRequest struct to add node ssh information
type AddSSHIdentityRequest struct {
	// Node ID
	// Required: true
	NID uint64 `url:"node_id" json:"node_id" validate:"required"`

	// Host name of the client
	// Required: true
	ClientHostName string `url:"client_host_name" json:"client_host_name" validate:"required"`

	// SSH host key of the client
	// Required: true
	ClientHostKey string `url:"client_host_key" json:"client_host_key" validate:"required" `

	// SSH public key of the client
	// Required: true
	ClientPublicKey string `url:"client_public_key" json:"client_public_key" validate:"required"`

	// IPv4 address
	// Required: true
	ClientIPAddress string `url:"client_ip_address" json:"client_ip_address" validate:"required" `
}

// AddSSHIdentity adds node ssh information
func (n Node) AddSSHIdentity(ctx context.Context, req AddSSHIdentityRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/node/add_ssh_identity"

	res, err := n.client.DecortApiCall(ctx, http.MethodPost, url, req)
	if err != nil {
		return false, err
	}

	result, err := strconv.ParseBool(string(res))
	if err != nil {
		return false, err
	}

	return result, nil
}
