"""Run conjecture 3.6 test specifically on n=15 with progress output."""
from sage.all import Graph
from sage.graphs.graph_generators import graphs
import sys

# Reuse helpers from check_conj_face_kempe.py by importing
sys.path.insert(0, '/Users/didericis/Code/math-research/papers/face_monochromatic_pairs/experiments')
from check_conj_face_kempe import (
    dual_of, apply_reduction, proper_3_edge_colorings,
    kempe_cycle, edge_idx, matches_chord_apex_kempe,
    conjecture_holds_for
)


def main():
    n = 15
    print(f"=== n = {n} ===")
    triangulations = list(graphs.triangulations(n, minimum_degree=5))
    print(f"  Found {len(triangulations)} triangulations.")
    total_cand = 0
    total_pass = 0
    total_fail = 0
    for tri_idx, G in enumerate(triangulations, start=1):
        G.is_planar(set_embedding=True)
        D = dual_of(G); D.is_planar(set_embedding=True)
        pentagonal_faces = [f for f in D.faces() if len(f) == 5]
        for face_no, face in enumerate(pentagonal_faces):
            for i_red in range(5):
                res = apply_reduction(D, face, i_red, 9999)
                if res is None: continue
                H = res['H']; named = res['named']
                H.is_planar(set_embedding=True)
                edges, colorings = proper_3_edge_colorings(H)
                cand = [c for c in colorings
                        if matches_chord_apex_kempe(edges, c, named)]
                if not cand: continue
                n_pass = sum(1 for c in cand
                             if conjecture_holds_for(H, edges, c, named)
                                is not None)
                n_fail = len(cand) - n_pass
                total_cand += len(cand)
                total_pass += n_pass
                total_fail += n_fail
                status = "PASSED" if n_fail == 0 else "*** FAILED ***"
                print(f"  tri#{tri_idx} face#{face_no} i_red={i_red}: "
                      f"{len(cand)} chord-apex+Kempe; {n_pass} pass, "
                      f"{n_fail} fail. {status}")
                sys.stdout.flush()
        if tri_idx % 5 == 0:
            print(f"  ... finished tri#{tri_idx}/{len(triangulations)}; "
                  f"cumulative cand={total_cand}, pass={total_pass}, "
                  f"fail={total_fail}")
            sys.stdout.flush()
    print(f"\nTotal: {total_cand} chord-apex+Kempe colorings tested; "
          f"{total_pass} pass, {total_fail} fail.")


if __name__ == '__main__':
    main()
