"""Reproduce the /data sample readout. Python standard library only."""
import csv
from collections import defaultdict
from pathlib import Path
import sys


def summarise(path):
    groups = defaultdict(list)
    with Path(path).open(encoding='utf-8', newline='') as source:
        for row in csv.DictReader(source):
            groups[(row['Banner'], row['Store ID'], row['Store City'])].append(row)
    for (banner, store, city), rows in sorted(groups.items()):
        promo = sum(row['On Promotion'] == 'true' for row in rows)
        unit = sum(row['Unit Price'] != '' for row in rows)
        yield (banner, store, city, len(rows), promo, unit)


if __name__ == '__main__':
    for banner, store, city, rows, promo, unit in summarise(sys.argv[1] if len(sys.argv) > 1 else 'observations.csv'):
        print(f'{banner} | {store} | {city} | {rows} rows | {promo} on promotion | {unit} with unit price')
    print('Blank On Promotion means not reported; it is not a regular price.')
