"""Owner-invoked notification test; never claim a crisis or completed liquidation."""
import argparse,json,sys
from pathlib import Path
from telemetry_notifier import ConsigliereTelemetryNotifier
from infinbot_common import strict_json,private_read,atomic_json,timestamp,Rejected


def main():
    p=argparse.ArgumentParser(description=__doc__);p.add_argument('--config',required=True)
    p.add_argument('--credentials',required=True);p.add_argument('--send-test',action='store_true');args=p.parse_args()
    if not args.send_test:
        print(json.dumps({'status':'NOT_SENT','reason':'Use --send-test locally for an owner-authorized test'}));return 1
    try:
        c=strict_json(private_read(args.config));n=ConsigliereTelemetryNotifier(args.credentials)
        if not n.dispatch_message('InFinBot notification delivery test. No crisis, orders or liquidation occurred.'):
            print(json.dumps(n.last_result));return 1
        received=sys.stdin.isatty() and input('Did this InFinBot test arrive on your actual phone? Type YES: ').strip()=='YES'
        result={'status':'NOTIFICATION_TEST_RECORDED','device_delivery':'OWNER_CONFIRMED' if received else 'UNVERIFIED',
                'observed_at':timestamp(),'api_status':'API_ACCEPTED','broker_liquidation_verified':False}
        path=Path(c['notification_receipt_file'])
        if path.exists():
            from uuid import uuid4
            atomic_json(path.with_name(path.stem+'-previous-'+uuid4().hex+'.json'),strict_json(private_read(path)))
        atomic_json(path,result,replace=True)
        print(json.dumps(result));return 0 if received else 1
    except Exception:print(json.dumps({'status':'NOTIFICATION_CHECK_BLOCKED','device_delivery':'UNVERIFIED'}));return 1


if __name__=='__main__':raise SystemExit(main())
