import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;

// Classifies a manually checked trace; does not call a vector store or LLM.
public class RagFailureProbe {
    static final String[] KEYS = {"authorized", "chunkStored", "rawTextOk",
        "embeddingCompatible", "referenceInCandidates", "referenceInContext",
        "contextCurrent", "answerGrounded"};
    static boolean flag(Properties p, String key) {
        String value = p.getProperty(key);
        if (!"true".equals(value) && !"false".equals(value)) {
            throw new IllegalArgumentException("Missing/invalid boolean: " + key);
        }
        return Boolean.parseBoolean(value);
    }
    static String classify(Properties p) {
        // Validate the entire input: missing evidence must not silently mean false.
        for (String key : KEYS) flag(p, key);
        if (!flag(p, "authorized")) return "DENY";
        if (!flag(p, "chunkStored") || !flag(p, "rawTextOk")) return "INGEST";
        if (!flag(p, "embeddingCompatible")) return "EMBEDDING";
        if (!flag(p, "referenceInCandidates")) return "RETRIEVE";
        if (!flag(p, "referenceInContext")) return "ASSEMBLY";
        if (!flag(p, "contextCurrent")) return "VERSION";
        if (!flag(p, "answerGrounded")) return "GENERATE";
        return "PASS";
    }
    public static void main(String[] args) throws Exception {
        if (args.length != 1) throw new IllegalArgumentException("Usage: RagFailureProbe trace.properties");
        Properties p = new Properties();
        try (Reader reader = Files.newBufferedReader(Path.of(args[0]), StandardCharsets.UTF_8)) {
            p.load(reader);
        }
        System.out.println("case=" + p.getProperty("caseId", "unnamed") + " branch=" + classify(p));
    }
}
