#!/usr/bin/env python3
"""
DVeProto 1.5 LTS — package entry (binary wire = 1.4 / ver=0x14).

Same cryptography and frame layout as wire 1.4. Prefer select "1.5" when offered.
Full package lives in ./reference.py (PACKAGE_VERSION = "1.5").
Wire-pinned 1.4 standalone: ./reference-1.4.py

  pip install cryptography
  from reference import DVeClientSession  # or run this file as a thin prefer=1.5 helper

DVeProto 1.5 LTS is the final release of the first-generation transport protocol.
Future network-layer features are developed in DVeProto 2.x.
"""
from __future__ import annotations

import importlib.util
import json
import sys
from pathlib import Path

_HERE = Path(__file__).resolve().parent
_REF = _HERE / "reference.py"


def _load_reference():
    spec = importlib.util.spec_from_file_location("dveproto_reference", _REF)
    if spec is None or spec.loader is None:
        raise ImportError(f"cannot load {_REF}")
    mod = importlib.util.module_from_spec(spec)
    sys.modules["dveproto_reference"] = mod
    spec.loader.exec_module(mod)
    return mod


_ref = _load_reference()

PACKAGE_VERSION = "1.5"
WIRE = "1.5"
BIN_VER = 0x14

DVeClientSession = _ref.DVeClientSession
DVeSession = _ref.DVeSession
DVeTicketStore = _ref.DVeTicketStore
WIRE_V15 = _ref.WIRE_V15
WIRE_V14 = _ref.WIRE_V14
server_hello_payload = _ref.server_hello_payload
parse_client_ack = _ref.parse_client_ack


def handshake_prefer_lts(hello_text: str):
    """Client handshake preferring 1.5 LTS (binary session wire = 1.4)."""
    return DVeClientSession.from_server_hello_text(hello_text, prefer=WIRE_V15)


if __name__ == "__main__":
    from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey

    priv = X25519PrivateKey.generate()
    hello = server_hello_payload(priv)
    sess, ack = handshake_prefer_lts(hello)
    print(
        json.dumps(
            {
                "package": PACKAGE_VERSION,
                "select": json.loads(ack).get("select"),
                "wire": sess.wire_version,
                "binary_ver": hex(BIN_VER),
            },
            ensure_ascii=False,
        )
    )
