"""Example supercell animations: for each priority salt, the two modes of the top dense Bloch-group pair at sigma 5 whose
q is not Gamma (and the top non-Gamma mode by participation otherwise). Uses build_supercell_animation.py.

    python make_supercell_examples.py PKG_DIR
"""
import csv
import json
import subprocess
import sys
from pathlib import Path

pkg = Path(sys.argv[1]).resolve()
for salt in ["SEED", "R3_F", "R4_pCl", "R4_pBr", "R5_pI"]:
    sel = json.loads((pkg / "modes" / salt / "selected_modes_dense_main.json").read_text())
    have = {m["mode_id"]: m for m in sel["modes"]}
    rows = [r for r in csv.DictReader(open(pkg / "contributions" / salt / "top_pairs.csv")) if r["condition_id"] == f"{salt}|dense|main|s5"]
    picks = []
    for r in rows:
        for mid in (r["mode_id_absorbed"], r["mode_id_emitted"]):
            if mid in have and ":q0.0.0:" not in mid and mid not in picks:
                picks.append(mid)
        if len(picks) >= 2:
            break
    out = pkg / "modes" / salt / "anim_supercell"
    out.mkdir(exist_ok=True)
    for mid in picks:
        fn = out / (mid.replace(":", "_") + ".extxyz")
        subprocess.run([sys.executable, str(Path(__file__).parent / "build_supercell_animation.py"),
                        str(pkg / "modes" / salt / "selected_modes_dense_main.json"), mid, str(fn), "--frames", "16", "--deposited-frame"], check=True)
        print(salt, mid, fn.stat().st_size)
