package segments

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

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

// GetFAARequest struct to get the floating/anycast IP address of a segment
type GetFAARequest struct {
	// ID of segment
	// Required: true
	SegmentID string `url:"segment_id" json:"segment_id" validate:"required"`
}

// GetFAA gets the floating/anycast IP address info for a segment
func (s Segments) GetFAA(ctx context.Context, req GetFAARequest) (*GetFAAResponse, error) {
	res, err := s.GetFAARaw(ctx, req)
	if err != nil {
		return nil, err
	}

	info := GetFAAResponse{}

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

	return &info, nil
}

// GetFAARaw gets the floating/anycast IP address info for a segment as an array of bytes
func (s Segments) GetFAARaw(ctx context.Context, req GetFAARequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/segment/get_faa"

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