package ai.testers;

import java.util.ArrayList;
import java.util.List;

public final class AnalysisResult {
    public final String kind;
    public boolean ok = true;
    public boolean skipped = false;
    public String reason;
    public String error;
    public int durationMs;
    public final List<Issue> issues = new ArrayList<>();
    public String raw;

    AnalysisResult(String kind) { this.kind = kind; }

    public boolean failed() {
        for (Issue i : issues) if ("high".equals(i.severity) || "critical".equals(i.severity)) return true;
        return false;
    }

    static AnalysisResult skipped(String kind, String reason, String error, int ms) {
        AnalysisResult r = new AnalysisResult(kind);
        r.skipped = true; r.reason = reason; r.error = error; r.durationMs = ms;
        r.ok = error == null;
        return r;
    }

    static AnalysisResult parse(String kind, String body, int ms) {
        AnalysisResult r = new AnalysisResult(kind);
        r.raw = body;
        r.durationMs = ms;
        for (Object o : Json.getArray(body, "issues")) {
            if (o instanceof java.util.Map<?,?> m) {
                Issue i = new Issue();
                i.severity = Json.s(m, "severity", "info");
                i.category = Json.s(m, "category", "general");
                i.message  = Json.s(m, "message", "");
                i.location = Json.s(m, "location", null);
                i.evidence = Json.s(m, "evidence", null);
                r.issues.add(i);
            }
        }
        return r;
    }

    public static final class Issue {
        public String severity, category, message, location, evidence;
    }
}
