package com.lingniu.ingest.yutongmqttapp;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

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

class YutongMqttPortainerComposeTest {

    @Test
    void portainerComposeUsesYutongMqttApplicationEnvironmentNames() throws IOException {
        String compose = Files.readString(repositoryRoot().resolve("deploy/portainer/docker-compose.yml"));

        assertThat(compose)
                .contains("KAFKA_TOPIC_YUTONG_MQTT_EVENT:")
                .contains("KAFKA_TOPIC_YUTONG_MQTT_RAW:")
                .contains("KAFKA_TOPIC_YUTONG_MQTT_DLQ:")
                .contains("YUTONG_MQTT_ENABLED: ${YUTONG_MQTT_ENABLED:-false}")
                .contains("YUTONG_MQTT_AUTO_STARTUP: ${YUTONG_MQTT_AUTO_STARTUP:-true}")
                .contains("YUTONG_MQTT_URI: ${YUTONG_MQTT_URI:-}")
                .contains("YUTONG_MQTT_TOPIC: \"${YUTONG_MQTT_TOPIC:-#}\"")
                .contains("YUTONG_MQTT_KEEP_ALIVE_SECONDS: ${YUTONG_MQTT_KEEP_ALIVE_SECONDS:-20}")
                .contains("YUTONG_MQTT_CONNECTION_TIMEOUT_SECONDS: ${YUTONG_MQTT_CONNECTION_TIMEOUT_SECONDS:-10}")
                .contains("YUTONG_MQTT_TLS_CA_PEM: ${YUTONG_MQTT_TLS_CA_PEM:-}")
                .contains("YUTONG_MQTT_TLS_CLIENT_PEM: ${YUTONG_MQTT_TLS_CLIENT_PEM:-}")
                .contains("YUTONG_MQTT_TLS_CLIENT_KEY: ${YUTONG_MQTT_TLS_CLIENT_KEY:-}")
                .contains("YUTONG_MQTT_TLS_HOSTNAME_VERIFICATION_ENABLED: ${YUTONG_MQTT_TLS_HOSTNAME_VERIFICATION_ENABLED:-true}")
                .doesNotContain("YUTONG_MQTT_URI: ${YUTONG_MQTT_URI:-tcp://127.0.0.1:1883}")
                .doesNotContain("KAFKA_TOPIC_PARTNER_EVENT")
                .doesNotContain("KAFKA_TOPIC_PARTNER_RAW")
                .doesNotContain("KAFKA_TOPIC_PARTNER_DLQ")
                .doesNotContain("\n      MQTT_ENABLED:")
                .doesNotContain("\n      MQTT_URI:")
                .doesNotContain("\n      MQTT_TOPIC:");
    }

    private static Path repositoryRoot() {
        Path current = Path.of(System.getProperty("user.dir")).toAbsolutePath();
        while (current != null) {
            if (Files.exists(current.resolve("deploy/portainer/docker-compose.yml"))) {
                return current;
            }
            current = current.getParent();
        }
        throw new IllegalStateException("repository root not found");
    }
}
