import java.nio.file.*;
import java.nio.charset.StandardCharsets;

public final class HistoricalReleaseRegressionGate {
  static Path root = Paths.get("..");
  static String read(String rel) throws Exception { return Files.readString(root.resolve(rel), StandardCharsets.UTF_8); }
  static void need(boolean ok,String msg){ if(!ok) throw new AssertionError(msg); }
  static void has(String s,String x,String msg){ need(s.contains(x),msg+" missing: "+x); }
  public static void main(String[] args) throws Exception {
    if(args.length>0) root=Paths.get(args[0]);
    String s14=read("SESSION14_TERMINAL_SETTLEMENT_AUDIT_v15.10.236.txt");
    has(s14,"Playable cards end at #57","S14 H10 bust boundary");
    has(s14,"#58 remains provenance-only for H10","S14 H10 post-bust provenance");
    has(s14,"H20 dealer remains 4C + 2H only","S14 H20 natural boundary");
    has(s14,"H21 begins from the corrected next real source card","S14 H21 chronology resume");

    String s5m=read("CLI/session_evidence/Session5_PreRender_Verified_Manifest.txt");
    String s5c=read("CLI/session_evidence/blackjack_observed_reconstruction_correction_20260906_195138.txt");
    has(s5m,"Action match/different 38/0","S5 observed Frozen action authority");
    has(s5c,"Directly Comparable Action Decisions: 28 | Matched 27 | Different 1 | Alignment 96.4%","S5 reconstructed comparison genuine divergence");

    String matrix=read("CLI/session_evidence/HISTORICAL_AUTHORITY_MATRIX.tsv");
    has(matrix,"6\t20260907_003015\tFIXED_CASUAL\tV9.2.54\tLEGACY_CHRONOLOGY_BOUNDARY","S6 chronology boundary");
    has(matrix,"7\t20260908_142730\tPUBLICATION\tV9.2.54\tLEGACY_ORPHAN","S7 orphan guard");
    need(!matrix.contains("7\t20260907_020601"),"S7 forbidden retained-journey substitution present");

    String s9=read("CLI/session_evidence/Session9_PreRender_Verified_Manifest.txt");
    has(s9,"same 27 | different 7 | decision alignment 79.4%","S9 27/7 forensic lock");

    String s10=read("CLI/session_evidence/blackjack_live_session_20260912_175237_CORRECTED_EVIDENCE.txt");
    has(s10,"H22 | PLAYER card 1          | 8♣","S10 split opening card 1");
    has(s10,"H22 | PLAYER card 2          | 8♠","S10 split opening card 2");
    has(s10,"H22 | PLAYER post-deal card  | 6C","S10 corrected split chronology");
    has(matrix,"10\t20260912_175237\tULTIMATE_AI\tV9.2.54\tHISTORICAL_DERIVED_AUTHORITATIVE","S10 Ultimate authority");

    String s12=read("CLI/session_evidence/Session12_PreRender_Verified_Manifest.txt");
    has(s12,"Frozen Reference: N/A | SOURCE_EXHAUSTED | NOT SOURCE-COMPLETE","S12 incomplete handling");
    String s13=read("CLI/session_evidence/Session13_PreRender_Verified_Manifest.txt");
    has(s13,"Frozen Reference: N/A | SOURCE_EXHAUSTED | NOT SOURCE-COMPLETE","S13 incomplete handling");

    has(matrix,"14\t20260920_003052\tULTIMATE_AI\tV9.2.67\tCHRONOLOGY_SENSITIVE_CANDIDATE","S14 Ultimate quarantine");
    has(matrix,"14\t20260920_003052\tFIXED_CASUAL\tV9.2.67\tCHRONOLOGY_SENSITIVE_CANDIDATE","S14 Fixed Casual quarantine");

    System.out.println("HISTORICAL RELEASE REGRESSION GATE: PASS");
    System.out.println("R1 S14 H10 bust/no post-bust dealer consumption: PASS");
    System.out.println("R2 S14 H20 natural/H21 corrected resume: PASS");
    System.out.println("R3 S5 genuine reconstructed divergence kept distinct from observed 38/0: PASS");
    System.out.println("R4 S6 legacy chronology boundary: PASS");
    System.out.println("R5 S7 orphan/substitution guard: PASS");
    System.out.println("R6 S9 27 SAME / 7 DIFFERENT forensic lock: PASS");
    System.out.println("R7 S10 H22 split + Ultimate authority: PASS");
    System.out.println("R8 S12/S13 source-exhausted N/A semantics: PASS");
    System.out.println("R9 S14 chronology-sensitive derived-controller quarantine: PASS");
  }
}
