package com.lingniu.ingest.vehiclestat;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;

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

class VehicleStatRepositoryContractTest {

    @Test
    void exposesOnlyMileageSampleAndQueryOperations() {
        assertThat(Arrays.stream(VehicleStatRepository.class.getDeclaredMethods())
                .map(Method::getName))
                .containsExactlyInAnyOrder("recordDailyMileageSample", "findDailyStat");
    }

    @Test
    void jt808MileagePlanDocumentsMetricTableDiffOnly() throws IOException {
        String plan = Files.readString(repositoryRoot()
                .resolve("docs/superpowers/plans/2026-06-30-kafka-streaming-mileage.md"));

        assertThat(plan)
                .contains("Only supported message backbone: Kafka.")
                .contains("Runtime state: none outside `vehicle_stat_metric`")
                .contains("daily_mileage_km = max_total_mileage_km - min_total_mileage_km")
                .contains("Do not create or write a protocol-specific JT808 daily-mileage table.")
                .doesNotContain("Redis `Jt808MileageStateStore`")
                .doesNotContain("VEHICLE_STAT_REPOSITORY_TYPE")
                .doesNotContain("REDIS_HOST")
                .doesNotContain("GPS segment distance")
                .doesNotContain("speed integral");
    }

    @Test
    void targetArchitectureDocumentsMetricTableAsOnlyJt808MileageRuntimeState() throws IOException {
        String architecture = Files.readString(repositoryRoot().resolve("docs/target-architecture.md"));

        assertThat(architecture)
                .contains("Only supported message backbone: Kafka.")
                .contains("Runtime state: none outside `vehicle_stat_metric`")
                .contains("Restart recovery reads the same metric row")
                .contains("JT808 daily mileage is stored only in `vehicle_stat_metric`")
                .doesNotContain("state-store")
                .doesNotContain("Redis mileage state")
                .doesNotContain("production memory");
    }

    @Test
    void jt808MileageRunbookDocumentsMetricTableAsOnlyRuntimeState() throws IOException {
        String runbook = Files.readString(repositoryRoot()
                .resolve("docs/operations/jt808-daily-mileage-runbook.md"));

        assertThat(runbook)
                .contains("Only supported message backbone: Kafka.")
                .contains("Runtime state: none outside `vehicle_stat_metric`")
                .contains("Restart recovery reads the same `daily_mileage_km` metric row")
                .doesNotContain("Redis mileage state")
                .doesNotContain("state-store");
    }

    private static Path repositoryRoot() {
        Path path = Path.of("").toAbsolutePath();
        while (path != null && !Files.exists(path.resolve("docs/superpowers/plans"))) {
            path = path.getParent();
        }
        if (path == null) {
            throw new IllegalStateException("repository root not found");
        }
        return path;
    }
}
