package com.lingniu.ingest.yutongmqttapp;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

import static org.assertj.core.api.Assertions.assertThat;

class YutongMqttAppDefaultsTest {

    @Test
    void applicationDefaultsKeepYutongMqttAsKafkaProducerOnly() throws IOException {
        Properties properties = applicationProperties();

        assertThat(properties)
                .containsEntry("spring.application.name", "yutong-mqtt-app")
                .containsEntry(
                        "spring.config.import[0]",
                        "optional:nacos:${spring.application.name}.${NACOS_CONFIG_FILE_EXTENSION:yml}?group=${NACOS_GROUP:DEFAULT_GROUP}&refreshEnabled=${NACOS_REFRESH_ENABLED:true}")
                .containsEntry("spring.cloud.nacos.config.server-addr", "${NACOS_SERVER_ADDR:127.0.0.1:8848}")
                .containsEntry("server.port", "${HTTP_PORT:20500}")
                .containsEntry("lingniu.ingest.mqtt.enabled", "${YUTONG_MQTT_ENABLED:false}")
                .containsEntry("lingniu.ingest.mqtt.auto-startup", "${YUTONG_MQTT_AUTO_STARTUP:true}")
                .containsEntry("lingniu.ingest.mqtt.endpoints[0].uri", "${YUTONG_MQTT_URI:}")
                .containsEntry("lingniu.ingest.mqtt.endpoints[0].topic", "${YUTONG_MQTT_TOPIC:#}")
                .containsEntry("lingniu.ingest.mqtt.endpoints[0].profile", "yutong")
                .containsEntry("lingniu.ingest.sink.kafka.enabled", true)
                .containsEntry("lingniu.ingest.sink.kafka.consumer.enabled", false)
                .containsEntry("lingniu.ingest.sink.kafka.topics.realtime", "${KAFKA_TOPIC_YUTONG_MQTT_EVENT:vehicle.event.mqtt-yutong.v1}")
                .containsEntry("lingniu.ingest.sink.kafka.topics.raw-archive", "${KAFKA_TOPIC_YUTONG_MQTT_RAW:vehicle.raw.mqtt-yutong.v1}")
                .containsEntry("lingniu.ingest.sink.kafka.topics.dlq", "${KAFKA_TOPIC_YUTONG_MQTT_DLQ:vehicle.dlq.mqtt-yutong.v1}")
                .containsEntry("lingniu.ingest.identity.store", "mysql")
                .containsEntry("lingniu.ingest.identity.mysql.table-name",
                        "${VEHICLE_IDENTITY_MYSQL_TABLE:vehicle_identity_bindings}")
                .containsEntry("lingniu.ingest.sink.archive.enabled", "${SINK_ARCHIVE_ENABLED:true}")
                .containsEntry("management.endpoints.web.exposure.include", "health,info,metrics,prometheus");

        assertThat(properties.stringPropertyNames())
                .noneMatch(name -> name.startsWith("lingniu.ingest.event-history."))
                .noneMatch(name -> name.startsWith("lingniu.ingest.event-file-store."))
                .noneMatch(name -> name.startsWith("lingniu.ingest.vehicle-state."))
                .noneMatch(name -> name.startsWith("lingniu.ingest.vehicle-stat."));
        assertThat(applicationYaml())
                .doesNotContain("event-history:")
                .doesNotContain("event-file-store:")
                .doesNotContain("vehicle-state:")
                .doesNotContain("vehicle-stat:")
                .doesNotContain("springdoc:")
                .doesNotContain("VEHICLE_IDENTITY_STORE")
                .doesNotContain("KAFKA_ENABLED");
    }

    private static Properties applicationProperties() {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(new ClassPathResource("application.yml"));

        Properties properties = factory.getObject();

        assertThat(properties).isNotNull();
        return properties;
    }

    private static String applicationYaml() throws IOException {
        return new String(new ClassPathResource("application.yml").getInputStream().readAllBytes(),
                StandardCharsets.UTF_8);
    }
}
