package logicalports

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

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

// GetRequest struct to get information about logical port
type GetRequest struct {
	// ID a logical port
	// Required: true
	ID string `url:"logical_port_id" json:"logical_port_id" validate:"required"`
}

// Get gets logical port details as a LogicalPort struct
func (a LogicalPorts) Get(ctx context.Context, req GetRequest) (*LogicalPort, error) {
	res, err := a.GetRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	info := LogicalPort{}

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

	return &info, nil

}

// GetRaw gets logical port details as an array of bytes
func (a LogicalPorts) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/logical_port/get"

	res, err := a.client.DecortApiCall(ctx, http.MethodGet, url, req)
	return res, err
}
