package com.lingniu.ingest.jt808app;

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

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

        assertThat(properties)
                .containsEntry("spring.application.name", "jt808-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("server.port", "${HTTP_PORT:20400}")
                .containsEntry("lingniu.ingest.jt808.enabled", true)
                .containsEntry("lingniu.ingest.jt808.port", "${JT808_PORT:808}")
                .containsEntry("lingniu.ingest.sink.kafka.enabled", true)
                .containsEntry("lingniu.ingest.sink.kafka.consumer.enabled", false)
                .containsEntry("lingniu.ingest.sink.kafka.topics.realtime", "${KAFKA_TOPIC_JT808_EVENT:vehicle.event.jt808.v1}")
                .containsEntry("lingniu.ingest.sink.kafka.topics.raw-archive", "${KAFKA_TOPIC_JT808_RAW:vehicle.raw.jt808.v1}")
                .containsEntry("lingniu.ingest.sink.kafka.topics.dlq", "${KAFKA_TOPIC_JT808_DLQ:vehicle.dlq.jt808.v1}")
                .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("lingniu.ingest.identity.mysql.jdbc-url",
                        "${VEHICLE_IDENTITY_MYSQL_JDBC_URL:jdbc:mysql://127.0.0.1:3306/lingniu_vehicle?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai}")
                .containsEntry("lingniu.ingest.identity.mysql.username", "${VEHICLE_IDENTITY_MYSQL_USERNAME:root}")
                .containsEntry("lingniu.ingest.identity.mysql.password", "${VEHICLE_IDENTITY_MYSQL_PASSWORD:}")
                .containsEntry("lingniu.ingest.identity.mysql.initialize-schema",
                        "${VEHICLE_IDENTITY_MYSQL_INITIALIZE_SCHEMA: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("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);
    }
}
