package ai.testers.junit5;

import ai.testers.AnalysisResult;
import ai.testers.Sinks;
import ai.testers.TestersAI;

import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public final class TestersAIFacade {
    private final TestersAI client;
    private final List<Consumer<AnalysisResult>> sinks;

    TestersAIFacade(TestersAI client, List<Consumer<AnalysisResult>> sinks) {
        this.client = client; this.sinks = sinks;
    }

    public AnalysisResult screenshot(Path p) throws Exception {
        return Sinks.dispatch(client.analyzeScreenshot(p, Map.of()), sinks);
    }
    public AnalysisResult screenshot(byte[] bytes, Map<String, Object> ctx) {
        return Sinks.dispatch(client.analyzeScreenshot(bytes, ctx), sinks);
    }
    public AnalysisResult consoleLogs(List<Map<String, Object>> entries, Map<String, Object> ctx) {
        return Sinks.dispatch(client.analyzeConsole(entries, ctx), sinks);
    }
    public AnalysisResult network(Object har, Map<String, Object> ctx) {
        return Sinks.dispatch(client.analyzeNetwork(har, ctx), sinks);
    }
    public AnalysisResult pageText(String text, Map<String, Object> ctx) {
        return Sinks.dispatch(client.analyzePageText(text, ctx), sinks);
    }
    public void assertClean(AnalysisResult r) {
        if (r.failed()) {
            StringBuilder sb = new StringBuilder("testersai found issues: ");
            for (AnalysisResult.Issue i : r.issues)
                sb.append('[').append(i.severity).append("] ").append(i.message).append("; ");
            throw new AssertionError(sb.toString());
        }
    }
}
