import subprocess, xml.etree.ElementTree as ET, time, sys

BGG_KEY = open('/tmp/.bggkey').read().strip()
HEADER = "Authorization: Bearer " + BGG_KEY

# Retry Valk search
time.sleep(3)
url = 'https://boardgamegeek.com/xmlapi2/search?query=Valk&type=boardgame'
r = subprocess.run(['curl', '-s', '-H', HEADER, url], capture_output=True, text=True, timeout=30)
print(f'Valk response length: {len(r.stdout)}')
if r.stdout.strip():
    root = ET.fromstring(r.stdout)
    items = root.findall('item')
    print(f'Total Valk items: {len(items)}')
    for item in items:
        n = item.find('name')
        name_val = n.get('value','') if n is not None else ''
        item_id = item.get('id','')
        if 'valkyo' in name_val.lower():
            print(f'  MATCH! id={item_id} name={name_val}')
else:
    print('EMPTY')

time.sleep(3)

# Try INSANITA with exact name search via thing API
url = 'https://boardgamegeek.com/xmlapi2/search?query=INSANIT%C3%80&type=boardgame&exact=1'
r = subprocess.run(['curl', '-s', '-H', HEADER, url], capture_output=True, text=True, timeout=30)
print(f'\nINSANITA exact search: {r.stdout[:500]}')

time.sleep(3)

# Try searching for "Insanity" (English version of Insanita)
url = 'https://boardgamegeek.com/xmlapi2/search?query=Insanity&type=boardgame'
r = subprocess.run(['curl', '-s', '-H', HEADER, url], capture_output=True, text=True, timeout=30)
root = ET.fromstring(r.stdout)
items = root.findall('item')
print(f'\nInsanity items: {len(items)}')
for item in items:
    n = item.find('name')
    name_val = n.get('value','') if n is not None else ''
    item_id = item.get('id','')
    if 'insanità' in name_val.lower() or 's.p.a' in name_val.lower() or 'spa' in name_val.lower():
        print(f'  MATCH? id={item_id} name={name_val}')
