package click.kamil.springstatemachineexporter.html;

import click.kamil.springstatemachineexporter.html.exporter.HtmlExporter;
import click.kamil.springstatemachineexporter.service.ExportService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

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

/**
 * Verifies that a finalized JSON export alone is sufficient for the HTML portal —
 * no source directory required when machine types and canonical identifiers are present.
 */
class JsonToHtmlInterchangeTest {

    private static final Path ORDER_GOLDEN_JSON = Path.of(
            "../state_machine_exporter/src/test/resources/golden/OrderStateMachineConfiguration/OrderStateMachineConfiguration.json")
            .toAbsolutePath()
            .normalize();

    @Test
    void shouldGenerateInteractiveHtmlFromJsonOnly(@TempDir Path tempDir) throws Exception {
        assertThat(ORDER_GOLDEN_JSON).exists();

        ExportService exportService = new ExportService(List.of(new HtmlExporter()));
        exportService.runJsonExporter(ORDER_GOLDEN_JSON, tempDir, List.of("html"), List.of());

        Path machineDir = tempDir.resolve("click.kamil.enterprise.machines.order.OrderStateMachineConfiguration");
        Path htmlFile = machineDir.resolve("click.kamil.enterprise.machines.order.OrderStateMachineConfiguration.html");
        assertThat(htmlFile).exists();

        String html = Files.readString(htmlFile);
        assertThat(html).contains("<!DOCTYPE html>");
        assertThat(html).contains("click.kamil.enterprise.machines.order.OrderStateMachineConfiguration");

        // SVG link IDs derive from fn-formatted enum labels; matchedTransitions use package-canonical values.
        assertThat(html).contains("OrderState_NEW__OrderEvent_PAY");
        assertThat(html).contains("click.kamil.enterprise.machines.order.OrderEvent.PAY");
        assertThat(html).contains("POST /api/machine/{machineType}/transition/{event}");
        assertThat(html).contains("\"stateTypeFqn\" : \"click.kamil.enterprise.machines.order.OrderState\"");
    }
}
