import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.*;
public class HistoricalEvidenceAudit {
  static class Row { int n; String id; Row(int n,String id){this.n=n;this.id=id;} }
  public static void main(String[] args) throws Exception {
    Path ev=Paths.get("..","CLI","session_evidence").normalize();
    if(!Files.isDirectory(ev)) ev=Paths.get("CLI","session_evidence").normalize();
    Path idx=ev.resolve("CANONICAL_PUBLICATION_SESSION_INDEX.tsv");
    if(!Files.exists(idx)) throw new IllegalStateException("Missing canonical publication index: "+idx);
    List<Row> rows=new ArrayList<>();
    for(String l:Files.readAllLines(idx,StandardCharsets.UTF_8)){
      if(l.trim().isEmpty()||l.startsWith("#"))continue; String[] p=l.split("\\t"); rows.add(new Row(Integer.parseInt(p[0]),p[1]));
    }
    int pass=0, legacy=0, orphan=0, fail=0;
    System.out.println("Deterministic Blackjack historical evidence audit — Phase 3 provenance gate");
    System.out.println("Publication identities: "+rows.size());
    for(Row r:rows){
      Path live=ev.resolve("blackjack_live_session_"+r.id+".txt");
      Path corr=ev.resolve("blackjack_live_session_"+r.id+"_CORRECTED_EVIDENCE.txt");
      boolean source=Files.exists(live)||Files.exists(corr);
      Path mf=ev.resolve("Session"+r.n+"_PreRender_Verified_Manifest.txt");
      if(!Files.exists(mf)){
        if(r.n<=2 && source){System.out.println("S"+r.n+" LEGACY-BOUNDARY — source present; pre-render manifest predates manifest workflow");legacy++;}
        else {System.out.println("S"+r.n+" FAIL — canonical source/manifest linkage incomplete for "+r.id+" (source="+source+", manifest=false)");fail++;}
        continue;
      }
      String t=Files.readString(mf,StandardCharsets.UTF_8); Matcher m=Pattern.compile("(?m)^SESSION:\\s*"+r.n+"\\s*\\|\\s*(\\d{8}_\\d{6})").matcher(t);
      String mid=m.find()?m.group(1):"MISSING";
      if(!r.id.equals(mid)||!source){
        if(r.n==7 && "20260908_142730".equals(r.id) && "20260907_020601".equals(mid) && !source){
          System.out.println("S7 LEGACY-ORPHAN — published plate="+r.id+"; retained Session7 manifest belongs to distinct journey "+mid+"; substitution BLOCKED"); orphan++; continue;
        }
        System.out.println("S"+r.n+" FAIL — plate/index="+r.id+" manifest="+mid+" sourcePresent="+source);fail++;continue;
      }
      Matcher o=Pattern.compile("Observed path:.*?hands (\\d+) \\| W/L/P (\\d+)/(\\d+)/(\\d+)").matcher(t);
      if(o.find()){
        int h=Integer.parseInt(o.group(1)), sum=Integer.parseInt(o.group(2))+Integer.parseInt(o.group(3))+Integer.parseInt(o.group(4));
        if(h!=sum){System.out.println("S"+r.n+" FAIL — observed W/L/P sum "+sum+" != hands "+h);fail++;continue;}
      }
      System.out.println("S"+r.n+" PASS — identity/source/manifest linkage");pass++;
    }
    System.out.println("SUMMARY: PASS="+pass+" LEGACY="+legacy+" LEGACY_ORPHAN="+orphan+" FAIL="+fail);
    if(fail>0) System.exit(2);
  }
}
