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

with open('/tmp/.bggkey') as f:
    bgg_key = f.read().strip()

HEADER = 'Authorization: Bearer *** + bgg_key
BASE_URL = 'https://boardgamegeek.com/xmlapi2/search'

def search_bgg(query_word):
    url = BASE_URL + '?query=' + query_word + '&type=boardgame'
    result = subprocess.run(['curl', '-s', '-H', HEADER, url], capture_output=True, text=True, timeout=30)
    if result.returncode != 0:
        return []
    xml_text = result.stdout.strip()
    if not xml_text:
        return []
    try:
        root = ET.fromstring(xml_text)
    except ET.ParseError:
        return []
    items = []
    for item in root.findall('item'):
        if item.get('type', '') != 'boardgame':
            continue
        name_el = item.find('name')
        name_val = name_el.get('value', '') if name_el is not None else ''
        year_el = item.find('yearpublished')
        year_val = year_el.get('value', '') if year_el is not None else ''
        items.append((item.get('id', ''), name_val, year_val))
    return items

# 1. Check ALL 41 results from Aeon's search for System Overload
print('=== Aeon's (all results, checking for System Overload) ===', file=sys.stderr)
items = search_bgg("Aeon's")
print('  Got ' + str(len(items)) + ' results', file=sys.stderr)
for i, (item_id, name, year) in enumerate(items):
    marker = ''
    if 'system overload' in name.lower():
        marker = ' <<< MATCH'
    if 'system' in name.lower() or 'overload' in name.lower():
        print('  [' + str(i) + '] id=' + item_id + ' name=' + repr(name) + ' year=' + year + marker, file=sys.stderr)
time.sleep(3)

# 2. Check ALL 82 results from Aeon search for System Overload
print('
=== Aeon (all results, checking for System Overload) ===', file=sys.stderr)
items = search_bgg('Aeon')
print('  Got ' + str(len(items)) + ' results', file=sys.stderr)
for i, (item_id, name, year) in enumerate(items):
    if 'system overload' in name.lower():
        print('  [' + str(i) + '] id=' + item_id + ' name=' + repr(name) + ' year=' + year + ' <<< MATCH', file=sys.stderr)
    elif 'system' in name.lower() and 'aeon' in name.lower():
        print('  [' + str(i) + '] id=' + item_id + ' name=' + repr(name) + ' year=' + year + ' (aeon+system)', file=sys.stderr)
time.sleep(3)

# 3. Check ALL 467 results from Ancient search for Ancient Blood
print('
=== Ancient (checking for Ancient Blood) ===', file=sys.stderr)
items = search_bgg('Ancient')
print('  Got ' + str(len(items)) + ' results', file=sys.stderr)
for i, (item_id, name, year) in enumerate(items):
    if name.lower().strip() == 'ancient blood':
        print('  [' + str(i) + '] id=' + item_id + ' name=' + repr(name) + ' year=' + year + ' <<< EXACT', file=sys.stderr)
    elif 'ancient blood' in name.lower():
        print('  [' + str(i) + '] id=' + item_id + ' name=' + repr(name) + ' year=' + year + ' <<< CONTAINS', file=sys.stderr)
time.sleep(3)

# 4. Check ALL 18 results from Believe search for Believe in me
print('
=== Believe (checking for Believe in me) ===', file=sys.stderr)
items = search_bgg('Believe')
print('  Got ' + str(len(items)) + ' results', file=sys.stderr)
for i, (item_id, name, year) in enumerate(items):
    if 'believe in me' in name.lower():
        print('  [' + str(i) + '] id=' + item_id + ' name=' + repr(name) + ' year=' + year + ' <<< MATCH', file=sys.stderr)
