"""Offline pairwise checker. Code: MIT; never executes model text."""
import argparse
import gzip
import json
from pathlib import Path


def score(item, arguments, config):
    original = arguments.get('original', {}).get('choice', 'invalid')
    swapped = arguments.get('swapped', {}).get('choice', 'invalid')
    allowed = ('A', 'B', 'equal', 'invalid')
    if original not in allowed:
        original = 'invalid'
    if swapped not in allowed:
        swapped = 'invalid'
    normalized = {'A': 'B', 'B': 'A'}.get(swapped, swapped)
    return {'gold': item['gold'], 'prediction': original,
            'secondary': {'agreement': {'gold': item['gold'], 'prediction': original},
                          'position_consistency': {'score': int(original != 'invalid' and original == normalized)},
                          'valid_output': {'score': int(original != 'invalid' and swapped != 'invalid')}}}


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Check scored records against fixture labels; no API calls.')
    parser.add_argument('raw', help='raw.jsonl.gz or scored.jsonl.gz')
    parser.add_argument('--fixture', default=str(Path(__file__).with_name('fixture.jsonl')))
    args = parser.parse_args()
    items = {r['item_id']: r for r in map(json.loads, Path(args.fixture).read_text().splitlines())}
    opener = gzip.open if args.raw.endswith('.gz') else open
    mismatches = 0
    with opener(args.raw, 'rt') as stream:
        for line in stream:
            row = json.loads(line)
            expected = score(items[row['item_id']], row['tool_arguments'], {})
            if expected['prediction'] != expected['gold']:
                mismatches += 1
                print(row['item_id'], 'repeat', row['repeat'], 'expected', expected['gold'], 'got', expected['prediction'])
            for key in expected:
                if key in row and row[key] != expected[key]:
                    raise SystemExit('Stored score differs from checker')
    print('Verified stored scores; label mismatches:', mismatches)
