package trunk

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

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

// GetRequest struct to get information about a trunk
type GetRequest struct {
	// ID of trunk
	// Required: true
	TrunkID uint64 `url:"id" json:"id" validate:"required"`
}

// Get gets detailed information about a trunk as a ItemTrunk struct
func (t Trunk) Get(ctx context.Context, req GetRequest) (*ItemTrunk, error) {
	res, err := t.GetRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	info := ItemTrunk{}

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

	return &info, nil
}

// GetRaw gets detailed information about a trunk as an array of bytes
func (t Trunk) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/trunk/get"

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