package influxdb

import (
	"io"
	"math"
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"
	"time"

	influx "github.com/influxdata/influxdb/client/v2"
	"github.com/prometheus/common/model"
	"github.com/stretchr/testify/require"
)

func TestClient(t *testing.T) {
	samples := model.Samples{
		{
			Metric: model.Metric{
				model.MetricNameLabel: "testmetric",
				"test_label":          "test_label_value1",
			},
			Timestamp: model.Time(123456789123),
			Value:     1.23,
		},
		{
			Metric: model.Metric{
				model.MetricNameLabel: "testmetric",
				"test_label":          "test_label_value2",
			},
			Timestamp: model.Time(123456789123),
			Value:     5.1234,
		},
		{
			Metric: model.Metric{
				model.MetricNameLabel: "nan_value",
			},
			Timestamp: model.Time(123456789123),
			Value:     model.SampleValue(math.NaN()),
		},
		{
			Metric: model.Metric{
				model.MetricNameLabel: "pos_inf_value",
			},
			Timestamp: model.Time(123456789123),
			Value:     model.SampleValue(math.Inf(1)),
		},
		{
			Metric: model.Metric{
				model.MetricNameLabel: "neg_inf_value",
			},
			Timestamp: model.Time(123456789123),
			Value:     model.SampleValue(math.Inf(-1)),
		},
	}

	expectedBody := `testmetric,test_label=test_label_value1 value=1.23 123456789123
testmetric,test_label=test_label_value2 value=5.1234 123456789123
`

	server := httptest.NewServer(http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			require.Equal(t, "POST", r.Method, "Unexpected method.")
			require.Equal(t, "/write", r.URL.Path, "Unexpected path.")
			b, err := io.ReadAll(r.Body)
			require.NoError(t, err, "Error reading body.")
			require.Equal(t, expectedBody, string(b), "Unexpected request body.")
		},
	))
	defer server.Close()

	serverURL, err := url.Parse(server.URL)
	require.NoError(t, err, "Unable to parse server URL.")

	conf := influx.HTTPConfig{
		Addr:     serverURL.String(),
		Username: "testuser",
		Password: "testpass",
		Timeout:  time.Minute,
	}
	c := NewClient(nil, conf, "test_db", "default")
	err = c.Write(samples)
	require.NoError(t, err, "Error sending samples.")
}
