package com.lingniu.ingest.gb32960app;

import com.lingniu.ingest.core.config.IngestCoreAutoConfiguration;
import com.lingniu.ingest.identity.MySqlVehicleIdentityService;
import com.lingniu.ingest.identity.config.VehicleIdentityAutoConfiguration;
import com.lingniu.ingest.protocol.gb32960.codec.Gb32960MessageDecoder;
import com.lingniu.ingest.protocol.gb32960.config.Gb32960AutoConfiguration;
import com.lingniu.ingest.protocol.gb32960.inbound.Gb32960NettyServer;
import com.lingniu.ingest.session.SessionStore;
import com.lingniu.ingest.session.config.SessionCoreAutoConfiguration;
import com.lingniu.ingest.sink.archive.ArchiveStore;
import com.lingniu.ingest.sink.archive.RawArchiveEventSink;
import com.lingniu.ingest.sink.archive.config.SinkArchiveAutoConfiguration;
import com.lingniu.ingest.sink.kafka.KafkaEventSink;
import com.lingniu.ingest.sink.kafka.KafkaEnvelopeDeadLetterSink;
import com.lingniu.ingest.sink.kafka.KafkaSinkAutoConfiguration;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.util.ClassUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

class Gb32960IngestAppCompositionTest {

    private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
            .withConfiguration(AutoConfigurations.of(
                    IngestCoreAutoConfiguration.class,
                    SessionCoreAutoConfiguration.class,
                    VehicleIdentityAutoConfiguration.class,
                    KafkaSinkAutoConfiguration.class,
                    SinkArchiveAutoConfiguration.class,
                    Gb32960AutoConfiguration.class))
            .withAllowBeanDefinitionOverriding(true)
            .withBean("kafkaProducer", KafkaProducer.class, Gb32960IngestAppCompositionTest::kafkaProducer)
            .withBean(SessionStore.class, () -> mock(SessionStore.class))
            .withPropertyValues(
                    "lingniu.ingest.gb32960.enabled=true",
                    "lingniu.ingest.gb32960.server.enabled=true",
                    "lingniu.ingest.gb32960.port=0",
                    "lingniu.ingest.identity.store=mysql",
                    "lingniu.ingest.identity.mysql.initialize-schema=false",
                    "lingniu.ingest.sink.kafka.enabled=true",
                    "lingniu.ingest.sink.kafka.bootstrap-servers=localhost:9092",
                    "lingniu.ingest.sink.kafka.consumer.enabled=false",
                    "lingniu.ingest.sink.archive.enabled=true",
                    "lingniu.ingest.sink.archive.path=target/test-archive-gb32960",
                    "lingniu.ingest.event-history.enabled=false");

    @Test
    void createsGb32960ListenerWithoutHistoryStorageBoundaries() {
        contextRunner.run(context -> {
            assertThat(context).hasSingleBean(Gb32960MessageDecoder.class);
            assertThat(context).hasSingleBean(Gb32960NettyServer.class);
            assertThat(context).hasSingleBean(MySqlVehicleIdentityService.class);
            assertThat(context).hasSingleBean(KafkaEventSink.class);
            assertThat(context).hasSingleBean(KafkaEnvelopeDeadLetterSink.class);
            assertThat(context).hasSingleBean(ArchiveStore.class);
            assertThat(context).hasSingleBean(RawArchiveEventSink.class);

            assertTypeNotPresent(context, "com.lingniu.ingest.eventfilestore.DuckDbParquetEventFileStore");
            assertTypeNotPresent(context, "com.lingniu.ingest.eventfilestore.config.EventFileStoreAutoConfiguration");
        });
    }

    @SuppressWarnings("unchecked")
    private static KafkaProducer<String, byte[]> kafkaProducer() {
        return mock(KafkaProducer.class);
    }

    private static void assertTypeNotPresent(ApplicationContext context, String className) {
        assertThat(ClassUtils.isPresent(className, context.getClassLoader()))
                .as("%s must stay outside the GB32960 ingest runtime", className)
                .isFalse();
    }
}
