package com.lingniu.ingest.gb32960app;

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 Gb32960IngestAppDefaultsTest {

    @Test
    void applicationDefaultsKeepGb32960IngestAsTcpKafkaProducerOnly() throws IOException {
        Properties properties = applicationProperties();
        String legacyKafkaPrefix = "lingniu.ingest.sink." + "mq.";
        String kafkaTypeProperty = "lingniu.ingest.sink.kafka." + "type";

        assertThat(properties)
                .containsEntry("spring.application.name", "gb32960-ingest-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("spring.data.redis.host", "${REDIS_HOST:127.0.0.1}")
                .containsEntry("spring.data.redis.port", "${REDIS_PORT:6379}")
                .containsEntry("spring.data.redis.database", "${REDIS_DATABASE:50}")
                .containsEntry("lingniu.ingest.gb32960.enabled", true)
                .containsEntry("lingniu.ingest.gb32960.server.enabled", true)
                .containsEntry("lingniu.ingest.gb32960.port", "${GB32960_PORT:32960}")
                .containsEntry("lingniu.ingest.gb32960.auth.platforms[0].username",
                        "${GB32960_PLATFORM_USER_HYUNDAI:Hyundai}")
                .containsEntry("lingniu.ingest.gb32960.auth.platforms[0].password",
                        "${GB32960_PLATFORM_PWD_HYUNDAI:}")
                .containsEntry("lingniu.ingest.gb32960.auth.platforms[1].username",
                        "${GB32960_PLATFORM_USER_YUEJIN:YueJin}")
                .containsEntry("lingniu.ingest.gb32960.auth.platforms[1].password",
                        "${GB32960_PLATFORM_PWD_YUEJIN:}")
                .containsEntry("lingniu.ingest.gb32960.vendor-extensions[0].match.platform-accounts[0]",
                        "Hyundai")
                .containsEntry("lingniu.ingest.gb32960.vendor-extensions[0].match.platform-accounts[1]",
                        "YueJin")
                .containsEntry("lingniu.ingest.sink.kafka.enabled", true)
                .containsEntry("lingniu.ingest.sink.kafka.consumer.enabled", false)
                .containsEntry("lingniu.ingest.sink.archive.enabled", "${SINK_ARCHIVE_ENABLED:true}")
                .containsEntry("lingniu.ingest.sink.archive.path", "${SINK_ARCHIVE_PATH:./archive/}")
                .containsEntry("lingniu.ingest.session.store", "redis")
                .containsEntry("lingniu.ingest.identity.store", "mysql")
                .containsEntry("lingniu.ingest.identity.mysql.table-name",
                        "${VEHICLE_IDENTITY_MYSQL_TABLE:vehicle_identity_bindings}")
                .containsEntry("management.endpoints.web.exposure.include",
                        "health,info,metrics,prometheus,gb32960sessions");

        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."))
                .noneMatch(name -> name.startsWith(legacyKafkaPrefix))
                .noneMatch(name -> name.equals(kafkaTypeProperty));
        assertThat(applicationYaml())
                .doesNotContain("event-history:")
                .doesNotContain("event-file-store:")
                .doesNotContain("vehicle-state:")
                .doesNotContain("vehicle-stat:")
                .doesNotContain("springdoc:");
        assertThat(applicationYaml())
                .contains("password: ${GB32960_PLATFORM_PWD_HYUNDAI:}")
                .contains("password: ${GB32960_PLATFORM_PWD_YUEJIN:}")
                .doesNotContain("SESSION_STORE")
                .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);
    }
}
