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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.support.messaging.exception.MessagingSchemaException;
import cz.moneta.test.harness.support.messaging.exception.MessagingTimeoutException;
import org.junit.jupiter.api.Disabled;

import java.util.concurrent.TimeUnit;


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

    @Disabled
    @TestCase(name = "Failed to connect")
    public void failedToConnect(Harness harness) {
        harness.putConfig("apiKey", "fero" );
        try {
            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();
        } catch (MessagingSchemaException e) {
            // "Failed to connect to Kafka cluster at pkc-xxxxx:9092: Authentication failed"
        }
    }

    @TestCase(name = "Wrong Schema")
    public void wrongSchema(Harness harness) {
        try {
            harness.withKafka()
                    .toTopic("nonexistent-topic")
                    .withKey("key")
                    .withPayload("{\"unknownField\": \"value\"}")
                    .send();
        } catch (MessagingSchemaException e) {
            // "Schema not found for subject 'nonexistent-topic-value' in Schema Registry"
            // "Payload does not conform to Avro schema for topic 'order-events': field 'orderId' is required"
        }
    }

    @TestCase(name = "Messaging timeout")
    public void messageTimeout(Harness harness) {
        try {
            harness.withKafka()
                    .fromTopic("order-events")
                    .receiveWhere(msg -> msg.extract("orderId").equals("nonexistent"))
                    .withTimeout(5, TimeUnit.SECONDS);
        } catch (MessagingTimeoutException e) {
            // "No message matching filter found on topic 'order-events' within 5 seconds"
            // "No message matching filter found on queue 'PAYMENT.NOTIFICATIONS' within 10 seconds"
        }
    }

    @Disabled
    @TestCase(name = "Ilegal state")
    public void ilegalState(Harness harness) {
        harness.putConfig("endpoints.kafka.bootstrap-servers", null);
        try {
            harness.withKafka()
                    .toTopic("order-events")
                    .withKey("key")
                    .withPayload("{}")
                    .send();
        } catch (IllegalStateException e) {
            // "You need to configure endpoints.kafka.bootstrap-servers"
            // Stejný vzor jako u Wso2GatewayEndpoint
        }
    }
}
