"""Compare thinking modes on exact-answer arithmetic tasks."""
import json
import os
from pathlib import Path
import sys
import time
from common import request_json

CASES = [
    ('What is 17 times 23?', '391'),
    ('What is (18 + 7) times 4?', '100'),
    ('A box holds 24 pencils. Five full boxes lose 17 pencils in total. How many pencils remain?', '103'),
    ('What is 125 minus 3 times 17?', '74'),
]


def compare(chat=request_json):
    rows = []
    for think in [False, True]:
        for question, expected in CASES:
            start = time.perf_counter()
            result = chat('chat', {
                'model': os.getenv('BLOG_CHAT_MODEL', 'qwen3:4b'),
                'stream': False, 'think': think,
                'messages': [{'role': 'user', 'content': question + ' Reply with only the final integer, no punctuation.'}],
                'options': {'temperature': 0, 'num_ctx': 4096, 'num_predict': 1024},
            })
            answer = result['message']['content'].strip()
            thinking = result['message'].get('thinking') or ''
            rows.append({
                'question': question, 'thinking': think, 'answer': answer,
                'expected': expected, 'exact_match': answer == expected,
                'seconds': round(time.perf_counter() - start, 3),
                'generated_tokens': result.get('eval_count'),
                # 'length' means the token limit stopped generation before the model finished.
                'done_reason': result.get('done_reason'),
                'has_thinking_field': bool(thinking),
                'thinking_chars': len(thinking),
                'content_chars': len(answer),
            })
    return rows


if __name__ == '__main__':
    rows = compare()
    output = Path(sys.argv[1] if len(sys.argv) > 1 else 'reasoning-results.json')
    output.write_text(json.dumps(rows, indent=2))
    for row in rows:
        print(json.dumps(row))
