#!/usr/bin/env python3
"""Offline-first sync parity check: two tills (the Windows and Android apps)
work offline, push later and pull each other's work, against a FRESH server.
Every /api/sync answer and the reports it feeds are recorded, so the Dart and
the Laravel servers can be compared field by field (values AND number types:
an app reading `qty as double` or `total as int` breaks if 2 becomes 2.0).

    python3 laravel_server/tools/sync_check.py http://127.0.0.1:8091 --trace dart.json
    python3 laravel_server/tools/sync_check.py http://127.0.0.1:8092 --trace php.json
    python3 laravel_server/tools/sync_check.py --compare dart.json php.json

Each server must be fresh (no super admin yet): the check sets one up.
"""
import json, sys, time, uuid, urllib.request, urllib.error

if '--compare' in sys.argv:
    a, b = sys.argv[sys.argv.index('--compare') + 1:][:2]
    A, B = json.load(open(a)), json.load(open(b))
    diffs = []

    def walk(x, y, path):
        if type(x) is not type(y):
            diffs.append(f'{path}: {x!r} ({type(x).__name__}) vs {y!r} ({type(y).__name__})')
        elif isinstance(x, dict):
            for k in sorted(set(x) | set(y)):
                if k not in x or k not in y:
                    diffs.append(f'{path}.{k}: only in {"first" if k in x else "second"}')
                else:
                    walk(x[k], y[k], f'{path}.{k}')
        elif isinstance(x, list):
            if len(x) != len(y):
                diffs.append(f'{path}: {len(x)} vs {len(y)} entries')
            for i, (p, q) in enumerate(zip(x, y)):
                walk(p, q, f'{path}[{i}]')
        elif x != y:
            diffs.append(f'{path}: {x!r} vs {y!r}')

    steps_a = {s['step']: s for s in A['steps']}
    steps_b = {s['step']: s for s in B['steps']}
    for name in steps_a:
        if name not in steps_b:
            diffs.append(f'{name}: missing in the second trace')
        else:
            walk(steps_a[name], steps_b[name], name)
    print(f"compared {len(steps_a)} recorded answers: {len(diffs)} differences")
    for d in diffs[:60]:
        print('  ', d)
    sys.exit(1 if diffs else 0)

BASE = sys.argv[1]
TRACE = sys.argv[sys.argv.index('--trace') + 1] if '--trace' in sys.argv else None
NOW = int(time.time() * 1000)
T0 = NOW - 2 * 86400000  # the offline sales happened two days ago
NS = uuid.UUID('6f1c2a7e-3b7d-4c47-9a51-000000000000')
ids = {}


def uid(name):
    """A fixed id for [name], the same on every run (so traces compare)."""
    ids[name] = ids.get(name) or str(uuid.uuid5(NS, name))
    return ids[name]


steps, problems, passed = [], [], 0
known_ids = {}


def norm(v):
    """Server-made ids, tokens and clock times become placeholders; numbers keep their type."""
    if isinstance(v, dict):
        return {k: norm(x) for k, x in v.items()}
    if isinstance(v, list):
        return [norm(x) for x in v]
    if isinstance(v, str):
        if v in known_ids:
            return known_ids[v]
        if len(v) == 36 and v.count('-') == 4:
            known_ids[v] = f'<id{len(known_ids)}>'
            return known_ids[v]
        if len(v) >= 32 and ' ' not in v and v.isascii() and any(c.isdigit() for c in v) and any(c.isalpha() for c in v):
            return '<token>'
        return v
    if isinstance(v, int) and not isinstance(v, bool) and v > 1_000_000_000_000:
        d = v - T0
        return f'T0+{d}' if 0 <= d <= 3 * 86400000 and d % 1000 == 0 else '<clock>'
    return v


def call(m, p, b=None, t=None, record=None):
    r = urllib.request.Request(BASE + p, method=m, data=None if b is None else json.dumps(b).encode(),
                               headers={'content-type': 'application/json', **({'authorization': 'Bearer ' + t} if t else {})})
    try:
        with urllib.request.urlopen(r, timeout=120) as res:
            s, body = res.status, json.loads(res.read() or b'null')
    except urllib.error.HTTPError as e:
        raw = e.read()
        try:
            s, body = e.code, json.loads(raw or b'null')
        except ValueError:
            s, body = e.code, raw.decode(errors='replace')
    if record:
        steps.append({'step': record, 'status': s, 'body': norm(body)})
    return s, body


def check(ok, what):
    global passed
    if ok:
        passed += 1
        print('ok  ', what)
    else:
        problems.append(what)
        print('FAIL', what)


def fig(rep, label):
    return next(f['value'] for f in rep['figures'] if f['label'] == label)


# --- a fresh platform and business -------------------------------------------------------
s, r = call('POST', '/api/auth/setup', {'email': 'root@sync.test', 'password': 'rootpass1'})
if s != 200:
    sys.exit('The server must be fresh (no super admin yet): ' + str(r))
root = r['token']
s, pkg = call('POST', '/api/admin/packages', {'name': 'Two tills', 'duration_days': 365, 'max_devices': 2,
                                              'features': ['reports', 'restaurant', 'inventory']}, root)
s, reg = call('POST', '/api/auth/register', {'business_name': 'Sync Grill', 'owner_name': 'Huda', 'email': 'owner@sync.test',
                                             'password': 'secret12', 'pin': '4321'})
owner, biz = reg['token'], reg['subscription']['business_id']
call('POST', f'/api/admin/businesses/{biz}/activate', {'package_id': pkg['id']}, root)
s, me = call('GET', '/api/me', None, owner)

# --- Till A (Windows) works offline, then pushes everything -------------------------------
def row(key, **v):
    return {'id': uid(key), 'updated_at': T0, 'deleted': 0, **v}


cat = row('cat', name='Grills', color=4280000000, sort=1)
kebab = row('kebab', name='Kebab plate', category_id=uid('cat'), price=2500, cost=900, track_stock=1, low_stock=2.0, image='', sku='KB1')
rice = row('rice', name='Rice by weight', category_id=uid('cat'), price=1200, cost=300, track_stock=1, low_stock=0.5, by_weight=1, unit='kg')
tea = row('tea', name='Karak tea', category_id=uid('cat'), price=300, track_stock=0, image='m/sample/tea.jpg')
cust = row('cust', name='Office Co', phone='0501112233')
stock_in = [row('in-kebab', item_id=uid('kebab'), qty=20.0, reason='purchase', created_at=T0 - 3600000),
            row('in-rice', item_id=uid('rice'), qty=10.0, reason='purchase', created_at=T0 - 3600000)]


def sale(n, at, lines, pay='cash', discount=0, customer=None):
    sub = sum(int(l[2] * l[3]) for l in lines)
    net = sub - discount
    tax = round(net * 5 / 100)
    rid = uid(f'r{n}')
    rec = row(f'r{n}', number=f'A-{1000 + n}', created_at=at, type='sale', employee_id=me.get('employee_id') or uid('emp'), customer_id=customer,
              device_id='till-a', subtotal=sub, discount=discount, tax=tax, total=net + tax, net=net, cost=0, payment_type=pay,
              tendered=net + tax, order_type='counter', points_earned=0, points_redeemed=0)
    rec['updated_at'] = at
    out_lines, moves = [], []
    for i, (item, name, qty, price) in enumerate(lines):
        total = int(qty * price)
        out_lines.append({**row(f'r{n}l{i}', receipt_id=rid, item_id=uid(item), name=name, qty=float(qty), price=price, cost=0, total=total,
                                discount=0, net=total, tax=round(total * 5 / 100), tax_category='standard'), 'updated_at': at})
        moves.append({**row(f'r{n}m{i}', item_id=uid(item), qty=-float(qty), reason='sale', ref_id=rid, created_at=at), 'updated_at': at})
    pay_row = {**row(f'r{n}p', kind='sale', receipt_id=rid, amount=net + tax, payment_type=pay, created_at=at, device_id='till-a'), 'updated_at': at}
    return rec, out_lines, moves, pay_row


r1 = sale(1, T0, [('kebab', 'Kebab plate', 2, 2500), ('rice', 'Rice by weight', 1.5, 1200)])
r2 = sale(2, T0 + 3600000, [('kebab', 'Kebab plate', 1, 2500), ('tea', 'Karak tea', 3, 300)], pay='card', discount=250, customer=uid('cust'))
pushA = {
    # Sent in an order different from syncOrder on purpose: the server applies syncOrder.
    'payments': [r1[3], r2[3]], 'receipt_lines': r1[1] + r2[1], 'receipts': [r1[0], r2[0]],
    'stock_movements': stock_in + r1[2] + r2[2], 'items': [kebab, rice, tea], 'categories': [cat], 'customers': [cust],
}
s, a1 = call('POST', '/api/sync', {'since': 0, 'device_id': 'till-a', 'device_name': 'Windows till', 'changes': pushA}, owner, 'A push offline work')
check(s == 200 and a1['more'] is False and a1['seq'] > 0, 'Windows till pushes its offline sales')
check(any(x['id'] == uid('r1') for x in a1['changes'].get('receipts', [])), 'the push answer includes the till\'s own rows (seq order)')
order = list(a1['changes'])
schema = json.load(open(__file__.rsplit('/', 3)[0] + '/shared/schema.json'))['sync_order']
check(order == [t for t in schema if t in order], 'tables come back in syncOrder')
seqA = a1['seq']
line = next(x for x in a1['changes']['receipt_lines'] if x['id'] == uid('r1l0'))
check(isinstance(line['qty'], float) and isinstance(line['total'], int), 'REAL columns come back as decimals, INTEGER as whole numbers')

# --- Till B (Android) starts empty and pulls everything -----------------------------------
s, b1 = call('POST', '/api/sync', {'since': 0, 'device_id': 'phone-b', 'device_name': 'Android phone', 'changes': {}}, owner, 'B full pull')
check(s == 200 and {x['id'] for x in b1['changes'].get('receipt_lines', [])} >= {uid('r1l0'), uid('r1l1'), uid('r2l1')}, 'Android till pulls the sales made on Windows')
seqB = b1['seq']
s, b2 = call('POST', '/api/sync', {'since': seqB, 'device_id': 'phone-b', 'changes': {}}, owner, 'B pull nothing new')
check(b2['changes'] == {} and b2['seq'] == seqB, 'nothing new: empty changes, same seq')
s, c1 = call('POST', '/api/sync', {'since': 0, 'device_id': 'third-c', 'changes': {}}, owner, 'C over device limit')
check(s == 403 and c1.get('code') == 'device_limit', 'a third device is refused (package allows 2)')

# --- the offline sales are in the reports and the stock ------------------------------------
q = f'from={T0 - 86400000}&to={NOW + 86400000}&tz=240'
s, rep = call('GET', f'/api/reports/sales_summary?{q}', None, owner, 'report sales summary')
check(fig(rep, 'Transactions') == 2 and fig(rep, 'Total sales incl. VAT') == r1[0]['total'] + r2[0]['total'], 'offline sales are in the Sales Summary')
s, st = call('GET', '/api/stock', None, owner, 'stock after offline sales')
level = {x['id']: x['in_stock'] for x in st} if isinstance(st, list) else {}
check(level.get(uid('kebab')) == 17 and level.get(uid('rice')) == 8.5, f'offline sales took the stock down (kebab {level.get(uid("kebab"))}, rice {level.get(uid("rice"))})')
s, rp = call('GET', f'/api/report?{q}', None, owner, 'dashboard report')
s, pt = call('GET', f'/api/reports/payment_types?{q}', None, owner, 'report payment types')
s, sv = call('GET', f'/api/reports/stock_valuation?{q}', None, owner, 'report stock valuation')
s, sm = call('GET', f'/api/reports/stock_movement?{q}', None, owner, 'report stock movement')

# --- conflicts: last write wins on updated_at ----------------------------------------------
stale = {**kebab, 'price': 9999, 'updated_at': T0 - 1000}
s, b3 = call('POST', '/api/sync', {'since': seqB, 'device_id': 'phone-b', 'changes': {'items': [stale]}}, owner, 'B pushes an older edit')
s, items = call('GET', '/api/data/items', None, owner)
check(next(i for i in items if i['id'] == uid('kebab'))['price'] == 2500, 'an older edit from a till does not overwrite a newer one')
fresh = {**kebab, 'price': 2700, 'updated_at': T0 + 7200000}
s, b4 = call('POST', '/api/sync', {'since': b3['seq'], 'device_id': 'phone-b', 'changes': {'items': [fresh]}}, owner, 'B pushes a newer edit')
s, a2 = call('POST', '/api/sync', {'since': seqA, 'device_id': 'till-a', 'changes': {}}, owner, 'A pulls B\'s edit')
check([i['price'] for i in a2['changes'].get('items', [])] == [2700], 'the Windows till receives the Android edit, once')

# --- an older app version leaves out newer columns: the server keeps them ------------------
old_app = {k: v for k, v in tea.items() if k not in ('image', 'name_ar', 'allergens')}
old_app.update(name='Karak tea (large)', updated_at=T0 + 7200000)
s, b5 = call('POST', '/api/sync', {'since': b4['seq'], 'device_id': 'phone-b', 'changes': {'items': [old_app]}}, owner, 'old app pushes without image')
s, items = call('GET', '/api/data/items', None, owner)
t = next(i for i in items if i['id'] == uid('tea'))
check(t['name'] == 'Karak tea (large)' and t['image'] == 'm/sample/tea.jpg', 'a column the old app does not know is kept')

# --- deleting: the deleted flag syncs and reports leave the bill out -----------------------
gone = [{**r2[0], 'deleted': 1, 'updated_at': T0 + 9000000}]
gone_moves = [{**m, 'deleted': 1, 'updated_at': T0 + 9000000} for m in r2[2]]
s, a3 = call('POST', '/api/sync', {'since': a2['seq'], 'device_id': 'till-a', 'changes': {'receipts': gone, 'stock_movements': gone_moves}}, owner, 'A deletes a bill')
s, b6 = call('POST', '/api/sync', {'since': b5['seq'], 'device_id': 'phone-b', 'changes': {}}, owner, 'B pulls the deletion')
check(any(x['id'] == uid('r2') and x['deleted'] == 1 for x in b6['changes'].get('receipts', [])), 'the deletion reaches the other till')
s, rep2 = call('GET', f'/api/reports/sales_summary?{q}', None, owner, 'report after deletion')
check(fig(rep2, 'Transactions') == 1, 'a deleted bill leaves the reports')
s, st2 = call('GET', '/api/stock', None, owner, 'stock after deletion')
check({x['id']: x['in_stock'] for x in st2}.get(uid('kebab')) == 18, 'a deleted bill gives its stock back')

# --- rows the server must ignore ------------------------------------------------------------
junk = {'nonsense_table': [{'id': 'x', 'updated_at': NOW}], 'items': [{'name': 'no id', 'updated_at': NOW}],
        'customers': [{**row('cust2', name='Extra Co', phone='0502223344'), 'favourite_colour': 'blue'}]}
s, a4 = call('POST', '/api/sync', {'since': a3['seq'], 'device_id': 'till-a', 'changes': junk}, owner, 'A pushes junk')
got = [c for c in a4['changes'].get('customers', []) if c['id'] == uid('cust2')]
check(s == 200 and got and 'favourite_colour' not in got[0] and 'nonsense_table' not in a4['changes'], 'unknown tables, rows without an id and unknown columns are ignored')

# --- paging: 2,000 changes per answer, "more" until done -----------------------------------
many = [row(f'bulk{i}', name=f'Walk-in {i}', phone=f'05{i:08d}') for i in range(2300)]
s, a5 = call('POST', '/api/sync', {'since': a4['seq'], 'device_id': 'till-a', 'changes': {'customers': many}}, owner)
check(s == 200 and a5['more'] is True and a5['seq'] == a4['seq'] + 2000, 'a big push answers with the first 2,000 changes and more=true')
since, seen, pages = b6['seq'], set(), 0
while True:
    s, pg = call('POST', '/api/sync', {'since': since, 'device_id': 'phone-b', 'changes': {}}, owner, f'B page {pages}')
    seen |= {c['id'] for c in pg['changes'].get('customers', [])}
    since, pages = pg['seq'], pages + 1
    if not pg['more'] or pages > 10:
        break
check(pages == 2 and {r['id'] for r in many} <= seen, 'the other till gets every change, in pages')

# --- a staff PIN session (web POS) may only push till work -----------------------------------
call('POST', '/api/data/settings', {'id': 'pin_login', 'value': '1'}, owner)
call('POST', '/api/data/settings', {'id': 'pin_login_devices', 'value': '0'}, owner)
s, info = call('GET', '/api/staff-login', None, owner)
s, pin = call('POST', '/api/auth/pin', {'code': info['code'], 'pin': '4321'})
staff = pin.get('token', '')
r3 = sale(3, T0 + 10800000, [('kebab', 'Kebab plate', 1, 2700)])
s, p1 = call('POST', '/api/sync', {'since': since, 'changes': {'receipts': [r3[0]], 'receipt_lines': r3[1], 'items': [{**kebab, 'price': 1, 'updated_at': NOW}]}},
             staff, 'staff session pushes')
s, items = call('GET', '/api/data/items', None, owner)
s, rep3 = call('GET', f'/api/reports/sales_summary?{q}', None, owner, 'report after staff sale')
check(next(i for i in items if i['id'] == uid('kebab'))['price'] == 2700 and fig(rep3, 'Transactions') == 2,
      'a PIN session pushes bills but cannot change the menu')

print(f'\n{passed} checks passed' + (f', {len(problems)} failed: ' + '; '.join(problems) if problems else ', all sync rules hold.'))
if TRACE:
    json.dump({'base': BASE, 'steps': steps}, open(TRACE, 'w'), indent=1, sort_keys=True)
    print(f'{len(steps)} answers recorded in {TRACE}')
sys.exit(1 if problems else 0)
