"""
cli.py — Main entry point and command dispatcher for onefirewall-ai.

Usage:
    onefirewall-ai                 → help
    onefirewall-ai help            → help
    onefirewall-ai version         → version
    onefirewall-ai login           → login flow
    onefirewall-ai <message...>    → stream chat response
"""

import sys

from onefirewall_ai.config import init_home


def main() -> None:
    """Entry point registered in pyproject.toml."""
    # Always ensure ~/.onefirewall-ai/ exists
    init_home()

    args = sys.argv[1:]  # everything after "onefirewall-ai"

    # ── No args or explicit help ───────────────────────────────────────────────
    if not args or args[0] in ("help", "--help", "-h"):
        from onefirewall_ai.utils import print_help
        print_help()
        return

    # ── Version ───────────────────────────────────────────────────────────────
    if args[0] in ("version", "--version", "-v"):
        from onefirewall_ai.utils import print_version
        print_version()
        return

    # ── Login ─────────────────────────────────────────────────────────────────
    if args[0] == "login":
        from onefirewall_ai.auth import login
        login()
        return

    # ── Chat — any other argument(s) become the prompt ────────────────────────
    prompt = " ".join(args).strip()
    if prompt:
        from onefirewall_ai.chat import send
        send(prompt)
        return

    # Fallback
    from onefirewall_ai.utils import print_help
    print_help()


if __name__ == "__main__":
    main()
