#!/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 1: Concordia Special Edition - check all 14 results for exact match
print('=== Fix: Concordia Special Edition ===', file=sys.stderr)
items = search_bgg('Concordia')
for item_id, name, year in items:
    print('  id=' + item_id + ' name=' + repr(name) + ' year=' + year, file=sys.stderr)
for item_id, name, year in items:
    if 'special' in name.lower() or 'edition' in name.lower():
        print('  FOUND SPECIAL: ' + item_id + ' ' + name, file=sys.stderr)

time.sleep(3)

# Fix 2: Aeon's End: System Overload - try fallback 'Overload'
print('\n=== Fix: Aeon\'s End: System Overload (fallback: Overload) ===', file=sys.stderr)
items = search_bgg('Overload')
for item_id, name, year in items:
    print('  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, file=sys.stderr)

time.sleep(3)

# Fix 3: Ancient Blood - try fallback 'Blood'
print('\n=== Fix: Ancient Blood (fallback: Blood) ===', file=sys.stderr)
items = search_bgg('Blood')
for i, (item_id, name, year) in enumerate(items[:20]):
    print('  [' + str(i) + '] id=' + item_id + ' name=' + repr(name) + ' year=' + year, file=sys.stderr)
for item_id, name, year in items:
    if name.lower().strip() == 'ancient blood':
        print('  EXACT MATCH: ' + item_id + ' ' + name + ' ' + year, file=sys.stderr)

time.sleep(3)

# Fix 4: Believe in me! - check all 18 results for exact match
print('\n=== Fix: Believe in me! (check all results) ===', file=sys.stderr)
items = search_bgg('Believe')
for item_id, name, year in items:
    print('  id=' + item_id + ' name=' + repr(name) + ' year=' + year, file=sys.stderr)
for item_id, name, year in items:
    if 'believe in me' in name.lower():
        print('  FOUND: ' + item_id + ' ' + name + ' ' + year, file=sys.stderr)

time.sleep(3)

# Fix 5: Wilderfeast - try alternative search
print('\n=== Fix: Wilderfeast (try wilder) ===', file=sys.stderr)
items = search_bgg('wilder')
for item_id, name, year in items:
    print('  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)
