"""Entry point for the XLTPrivilege Python demo.

Mirrors `demo/xlt_privilege_api_demo/main.cpp`.

Run (from this directory):
    python runme.py
"""

from __future__ import annotations

import logging
import signal
import py_xlt_api as xlt

try:
    from privilege_demo import PrivilegeAccount, XLTPrivilegeDemo, default_config
except Exception:
    # Allow running as a module/package too.
    from .privilege_demo import PrivilegeAccount, XLTPrivilegeDemo, default_config


def main() -> int:
    # Keep log lines printable; callback messages are designed to match C++ printf.
    logging.basicConfig(level=logging.INFO, format="%(message)s")

    demo = XLTPrivilegeDemo()
    print("API version: %s" % demo.api_version())

    # Handle SIGTERM (e.g. ``kill <pid>``).
    # demo.shutdown() calls os._exit(0) internally, so we never return.
    def _on_signal(_sig, _frame):
        print("\nSignal %d received, shutting down ..." % _sig)
        demo.shutdown()          # calls os._exit(0)

    signal.signal(signal.SIGTERM, _on_signal)

    demo = XLTPrivilegeDemo()
    print("API version: %s" % demo.api_version())

    if not demo.initialize(default_config()):
        return -1

    account = PrivilegeAccount(
        account_name="admin",
        password="12345678",
        pub_key_base64="XXXXXXXXXXXXXX",
    )

    if not demo.login(account):
        return -1

    print("Trading day: %d" % demo.trading_day())

    if not demo.subscribe_trade_message():
        return -1

    # if not demo.query_assets(1391):
    #     return -1
    #
    # if not demo.query_positions(1391):
    #     return -1

    # Optional operations from the C++ main.cpp (previously commented out).
    # if not demo.query_orders(1391):
    #     return -1
    # if not demo.query_orders_by_page(1391):
    #     return -1
    # if not demo.query_trades(1391):
    #     return -1
    # if not demo.query_init_assets(1391):
    #     return -1
    # if not demo.query_init_positions(1391):
    #     return -1
    # if not demo.query_trade_account_info():
    #     return -1
    # if not demo.query_brief_assets(1391):
    #     return -1
    # if not demo.query_brief_positions(1391):
    #     return -1
    # if not demo.dump_final_file():
    #     return -1



    # Counter fund transfer demo (same example values as C++ main.cpp)
    # if not demo.submit_counter_fund_transfer_request("BIZ20260310001", 3, 1, 2000000):
    #     return -1
    # if not demo.submit_counter_fund_transfer_result(0, "BIZ20260310001", 3, 1, 2000000, 1, ""):
    #     return -1
    #
    # # Cluster fund transfer demo
    # if not demo.submit_cluster_fund_transfer_request("CLUSTER001", 9, 2000000):
    #     return -1
    # if not demo.submit_cluster_fund_transfer_result(0, "CLUSTER001", 9, 2000000, 1, ""):
    #     return -1

    # Fund transfer exec request demo (active in C++ main.cpp)
    # if not demo.insert_fund_transfer_exec("EXEC20260319009", 1391, int(xlt.FundTransSide_IN), 4000000, 0, int(xlt.FundTransExecType_NORMAL)):
    #     return -1




    # Keep running like C++ demo.
    # run_forever() handles Ctrl+C → shutdown() → os._exit(0).
    demo.run_forever()
    return 0


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

