#!/usr/bin/env bash
# Batch open .fig files in Figma desktop app (→ Drafts)
# Mode: "all" (72 files) or "slim" (W37 only 1, rest all = 48 files)
set -euo pipefail
MODE="${1:-slim}"
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

python3 - "$MODE" <<'PY' > /tmp/fig-list.txt
import json, sys, os
mode = sys.argv[1]
m = json.load(open("manifest.json"))
already_opened = {"W1"}  # already done in earlier test
files = []
for t in m["templates"]:
    if not t["fig"]: continue
    figs = list(t["fig"])
    if mode == "slim" and t["id"] == "W37":
        figs = figs[:1]
    for f in figs:
        path = f'extracted/{t["id"]}/{f}'
        if t["id"] in already_opened and figs.index(f) == 0 and t["id"] == "W1":
            continue  # skip W1 first fig (already opened)
        files.append(path)
print('\n'.join(files))
PY

count=$(wc -l < /tmp/fig-list.txt | tr -d ' ')
echo "Mode=$MODE  Files to open: $count"
echo ""

i=0
while IFS= read -r f; do
  i=$((i+1))
  printf "[%2d/%d] %s\n" "$i" "$count" "$f"
  open -a Figma "$ROOT/$f"
  sleep 1.5
done < /tmp/fig-list.txt

echo ""
echo "Done. All $count .fig files sent to Figma."
