import plyvel
import json
import sys

def find_spells(db_path, spell_names):
    found = {}
    try:
        db = plyvel.DB(db_path, create_if_missing=False)
        for key, value in db:
            try:
                # Foundry data often starts with a single byte indicating type or metadata
                # Try to find JSON-like content
                start_idx = value.find(b'{')
                if start_idx != -1:
                    data = json.loads(value[start_idx:])
                    name = data.get('name')
                    if name in spell_names:
                        found[name] = data.get('_id')
            except Exception:
                continue
        db.close()
    except Exception as e:
        print(f"Error opening {db_path}: {e}")
    return found

spell_names = [
    "Protection from Evil and Good",
    "Zone of Truth",
    "Magic Circle",
    "Banishment",
    "Hold Monster"
]

paths = [
    "FoundryVTT/Data/modules/dnd-players-handbook/packs/spells",
    "FoundryVTT/Data/systems/dnd5e/packs/spells"
]

results = {}
for path in paths:
    print(f"Searching in {path}...")
    found = find_spells(path, spell_names)
    for name, _id in found.items():
        if name not in results:
            results[name] = {"id": _id, "pack": path}

print(json.dumps(results, indent=2))
