"""
統合CLIエントリーポイント。

`uvx rithmic-bridge publish ...` / `uvx rithmic-bridge client ...`
の形で既存サブCLIへ委譲する。
"""

from __future__ import annotations

import argparse
import sys
from typing import List, Optional

from . import __version__
from .client.cli import main as client_main
from .publisher.cli import main as publisher_main


def main(argv: Optional[List[str]] = None) -> None:
    """
    統合CLIのエントリーポイント。
    """
    if argv is None:
        argv = sys.argv[1:]

    parser = argparse.ArgumentParser(
        prog="rithmic-bridge",
        description="Rithmic Bridge 統合CLI",
    )
    parser.add_argument(
        "--version",
        action="version",
        version=f"%(prog)s {__version__}",
    )

    # まずは最小要件として publish/client の2系統に絞る
    if len(argv) == 0:
        parser.print_help()
        print("\nusage examples:")
        print("  rithmic-bridge publish --mode rapi")
        print("  rithmic-bridge client --host localhost --port 5555")
        return

    if argv[0] in ("-h", "--help"):
        parser.print_help()
        print("\ncommands:")
        print("  publish  Publisher server CLI")
        print("  client   Client CLI")
        print("\nusage examples:")
        print("  rithmic-bridge publish --mode rapi")
        print("  rithmic-bridge client --host localhost --port 5555")
        return

    if argv[0] in ("--version", "-V"):
        print(__version__)
        return

    command = argv[0]
    sub_argv = argv[1:]

    if command == "publish":
        publisher_main(sub_argv, prog="rithmic-bridge publish")
        return

    if command == "client":
        client_main(sub_argv, prog="rithmic-bridge client")
        return

    parser.error(f"unknown command: {command}")
