#!/usr/bin/env python3
"""Независимый фиксированный агрегатный эталон; без импорта evaluate.py."""
from pathlib import Path
from decimal import Decimal
import json,sys
p=Path(__file__).resolve().parent
r=json.loads((Path(sys.argv[1]) if len(sys.argv)>1 else p/'result.json').read_text(encoding='utf-8'))
early_t=40000-4000-18000-1800-1000;early_c=30000-1000-14000-1200
late_t=early_t-10800+5400-2000;late_c=early_c-1000+500-100
assert (early_t,early_c,late_t,late_c)==(15200,13800,7800,13200)
for day,t,c,diff in [('7',early_t,early_c,70),('30',late_t,late_c,-270)]:
 s=r['snapshots'][day];assert s['groups']['test']['contribution_RUB']==t;assert s['groups']['control']['contribution_RUB']==c
 assert s['groups']['test']['assigned']==s['groups']['control']['assigned']==20
 assert Decimal(s['difference_per_assigned_RUB'])==Decimal(diff)
 assert Decimal(s['groups']['test']['per_assigned_RUB'])==Decimal(t)/20
 assert Decimal(s['groups']['control']['per_assigned_RUB'])==Decimal(c)/20
 assert s['groups']['test']['ever_paid_buyers']==8 and s['groups']['control']['ever_paid_buyers']==6
assert r['unique_reached_test']==18
assert r['other_contact_in_control']==['C02','C19']
assert r['naive_join_gross_test_RUB']==46000 and r['correct_test_orders']==8
assert r['snapshots']['30']['groups']['test']['net_revenue_RUB']==25200
assert r['snapshots']['30']['groups']['control']['net_revenue_RUB']==28000
print('PASS: early/final economics, assigned denominators, reach vs assignment, control contamination, duplicate-join example.')
