"""Reproduce the guide's synthetic example; no model or external data."""
import csv
import io
import json
import platform
import re
from decimal import Decimal

CSV = 'urun_kodu;tutar_tl\n0012;1,25\n0013;2,50\n0014;\n'


def read(sep):
    return list(csv.reader(io.StringIO(CSV), delimiter=sep, strict=True))


wrong, correct = read(','), read(';')
header, *rows = correct
assert header == ['urun_kodu', 'tutar_tl']
assert all(len(row) == len(header) for row in rows)
assert [row[0] for row in rows] == ['0012', '0013', '0014']
amounts = []
for _, text in rows:
    if text == '':
        continue
    # This demo accepts only the exact notation declared for tutar_tl.
    if not re.fullmatch(r'[0-9]+,[0-9]{2}', text):
        raise ValueError(f'Unexpected amount: {text!r}')
    amounts.append(Decimal(text.replace(',', '.')))
total = sum(amounts, Decimal('0.00'))
assert (len(rows), len(amounts), total) == (3, 2, Decimal('3.75'))
assert wrong[1] == ['0012;1', '25'] and correct[1] == ['0012', '1,25']
print(json.dumps({'python': platform.python_version(), 'input': CSV,
                  'comma_fields': wrong, 'semicolon_fields': correct,
                  'records': len(rows), 'amounts_present': len(amounts),
                  'missing_amounts': len(rows) - len(amounts), 'subtotal_tl': str(total)},
                 ensure_ascii=False, indent=2))
