package cz.moneta.test.system.messaging.kafka;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import cz.moneta.test.dsl.Harness;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;

import java.util.Arrays;
import java.util.Map;


@TestScenario(name = "Test Kafka pruduce functions")
public class KafkaProducerTest {

    @TestCase(name = "Send message with standard headers")
    public void sendWithStandardHeaders(Harness harness) {
        harness.withKafka()
                .toTopic("order-events")
                .withKey("order-123")
                .withPayload("{\"orderId\": \"123\", \"status\": \"CREATED\"}")
                .withTraceparent("00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01")
                .withRequestID("req-001")
                .withActivityID("act-555")
                .withSourceCodebookId("CB-01")
                .send();
    }

    @TestCase(name = "Send message with additional headers")
    public void sendWithAdditionalHeaders(Harness harness) {
        harness.withKafka()
                .toTopic("order-events")
                .withKey("order-456")
                .withPayload("{\"orderId\": \"456\", \"status\": \"SHIPPED\"}")
                .withHeader("customHeader", "customValue")
                .send();
    }

    @TestCase(name = "Send message with additional headers")
    public void sendToDev(Harness harness) {
        harness.withKafka()
                .toTopic("TP_CVM_CARD-AUTHORIZATION-NOTIFICATION_STREAM_APP")
                .withKey("happyday_VZI23")
                .withPayloadFromFile("system/kafka/test.json")
                .withHeader("requestId", "c3e2d840d4c4d6f14040404040404040e254359aa23fad11")
                .withHeader("activityId", "FSVc3e2d840d4c4d6f14040404040404040e254359aa23fad11")
                .withHeader("traceparent", "00-c3e2d840d4c4d6f14040404040404040-e254359aa23fad11-01")
                .withHeader("sourceCodebookId", "KOD_ODS")
                .send();
    }

    @TestCase(name = "Send message loaded from file")
    public void sendLoadedMessage(Harness harness) {
        harness.withKafka()
                .toTopic("order-events")
                .withKey("order-789")
                .withPayloadFromFile("system/messaging/order_event.json")
                .withRequestID("req-002")
                .send();
    }

    @TestCase(name = "Send message with added paramaters")
    public void sendWithAddedParamaters(Harness harness) {
        harness.withKafka()
                .toTopic("order-events")
                .withKey("order-123")
                .withPayload("{}")
                .addField("orderId", "555")
                .addField("status", "CREATED")
                .addField("items", Arrays.asList(
                        Map.of("sku", "A001", "qty", 2),
                        Map.of("sku", "B002", "qty", 1)))
                .addField("items[1]", "note", "fragile")
                .withRequestID("req-001")
                .send();
    }

    @TestCase(name = "Send from file with added parameters")
    public void sendFromFileWithAddedParamaters(Harness harness) {
        harness.withKafka()
                .toTopic("order-events")
                .withKey("order-456")
                .withPayloadFromFile("system/messaging/order_event.json")
                .addField("metadata", JsonNodeFactory.instance.objectNode())
                .addField("metadata", "source", "test-harness")
                .addField("metadata", "timestamp", System.currentTimeMillis())
                .send();
    }

    @TestCase(name = "Send with added object in array")
    public void sendWithAddedObjectsInArray(Harness harness) {
        harness.withKafka()
                .toTopic("order-events")
                .withKey("order-789")
                .withPayloadFromFile("system/messaging/order_base.json")
                .appendToArray("items", Map.of("sku", "C003", "qty", 5))
                .send();
    }
}
