package kafka

import (
	"context"
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/testhelper"

	"github.com/confluentinc/confluent-kafka-go/v2/kafka"
)

var producerConfig = &kafka.ConfigMap{
	"socket.timeout.ms":  10,
	"message.timeout.ms": 10,
}

func TestProducer(t *testing.T) {
	p, err := kafka.NewProducer(producerConfig)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestProducer", nil, err)
	}

	producer := &Producer{producer: p}
	err = producer.Produce("gotopic", "key", "test", nil)
	if err == nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestProducer", "Local: Message timed out", err)
	}
}

func TestProducerError(t *testing.T) {
	p, err := kafka.NewProducer(producerConfig)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestProducer", nil, err)
	}

	producer := &Producer{producer: p}
	err = producer.Produce("", "key", "test", nil)
	if err == nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestProducer", "Local: Invalid argument of configuration", err)
	}
}

func TestProduceBinary(t *testing.T) {
	t.Skip()
	ctx := context.Background()
	producer, err := NewProducer(ctx)
	if err != nil {
		t.Error(err)
		return
	}

	err = producer.ProduceBinary("topic", "key", []byte("This is a test"), nil)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestProducer", "Local: Invalid argument of configuration", err)
	}

	err = producer.ProduceBinary("topic", "key", []byte("This is a test"), map[string][]byte{
		"key1": []byte("val1"),
		"key2": []byte("val2"),
	})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestProducer", "Local: Invalid argument of configuration", err)
	}
}
