#!/bin/bash
# Display results from the most recent test run for all engines.
# Usage: ./show-results.sh

TESTS_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(dirname "$TESTS_DIR")"

cd "$PROJECT_DIR" || exit 1

python3.8 -c "
import json, sys

for engine in ['claude', 'openai', 'gemini', 'xai']:
    try:
        with open('tests/log/{}/post_collation.json'.format(engine)) as f:
            d = json.load(f)
    except FileNotFoundError:
        print('{}: no results'.format(engine))
        print()
        continue

    o = d.get('output', {})
    table = o.get('table', [])
    witnesses = o.get('witnesses', [])
    fb = o.get('collation_feedback', {})
    usage = fb.get('engine_usage', {})
    comments = fb.get('comments', '')

    print('{}: {} CGs, {} witnesses — {}'.format(
        engine, len(table), len(witnesses), usage.get('summary', 'no usage')))

    if table and witnesses:
        for wi, wid in enumerate(witnesses):
            row = []
            for cg in table:
                cell = cg[wi] if wi < len(cg) else []
                if cell:
                    words = [t.get('original', t.get('t', '?')) if isinstance(t, dict) else str(t) for t in cell]
                    row.append(' '.join(words))
                else:
                    row.append('---')
            print('  {:45s} | {}'.format(wid, ' | '.join(row)))

    if comments:
        short = comments[:300] + '...' if len(comments) > 300 else comments
        # strip HTML for terminal display
        import re
        short = re.sub(r'<[^>]+>', ' ', short)
        short = re.sub(r'\s+', ' ', short).strip()
        print('  Comments: {}'.format(short))

    print()
"
