#!/usr/bin/env python3
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

# Fix: Aeon's End: System Overload - try 'System'
print('=== Fix: Aeon\'s End: System Overload (fallback: System) ===', file=sys.stderr)
items = search_bgg('System')
for i, (item_id, name, year) in enumerate(items[:30]):
    print('  [' + str(i) + '] id=' + item_id + ' name=' + repr(name) + ' year=' + year, file=sys.stderr)
for item_id, name, year in items:
    if 'system overload' in name.lower() and 'aeon' in name.lower():
        print('  FOUND: ' + item_id + ' ' + name + ' ' + year, file=sys.stderr)
# Also check for just 'system overload'
for item_id, name, year in items:
    if 'system overload' in name.lower():
        print('  FOUND (any): ' + item_id + ' ' + name + ' ' + year, file=sys.stderr)

time.sleep(3)

# Fix: Wilderfeast - try 'feast'
print('\n=== Fix: Wilderfeast (try feast) ===', file=sys.stderr)
items = search_bgg('feast')
for i, (item_id, name, year) in enumerate(items[:30]):
    print('  [' + str(i) + '] id=' + item_id + ' name=' + repr(name) + ' year=' + year, file=sys.stderr)
for item_id, name, year in items:
    if 'wilderfeast' in name.lower():
        print('  FOUND: ' + item_id + ' ' + name + ' ' + year, file=sys.stderr)

time.sleep(3)

# Fix: Verify A Life - search 'Life' and find exact 'A Life'
print('\n=== Verify: A Life ===', file=sys.stderr)
items = search_bgg('Life')
for item_id, name, year in items:
    if name.lower().strip() == 'a life':
        print('  EXACT: id=' + item_id + ' name=' + repr(name) + ' year=' + year, file=sys.stderr)

time.sleep(3)

# Fix: Verify Growing Season
print('\n=== Verify: Growing Season ===', file=sys.stderr)
items = search_bgg('Growing')
for item_id, name, year in items:
    if name.lower().strip() == 'growing season':
        print('  EXACT: id=' + item_id + ' name=' + repr(name) + ' year=' + year, file=sys.stderr)

time.sleep(3)

# Fix: Try 'Aeon' without apostrophe for Aeon's End: System Overload
print('\n=== Fix: Aeon\'s End: System Overload (try Aeon) ===', file=sys.stderr)
items = search_bgg('Aeon')
for i, (item_id, name, year) in enumerate(items[:30]):
    print('  [' + str(i) + '] id=' + item_id + ' name=' + repr(name) + ' year=' + year, file=sys.stderr)
for item_id, name, year in items:
    if 'system overload' in name.lower():
        print('  FOUND: ' + item_id + ' ' + name + ' ' + year, file=sys.stderr)
