"""Entry point for ``python -m cyclone``.

* ``python -m cyclone`` (no args) — Click CLI (``cli.main``)
* ``python -m cyclone serve``     — start the FastAPI app on 127.0.0.1:8000

Honors the env vars:

* ``CYCLONE_PORT`` (default ``8000``)
* ``CYCLONE_RELOAD`` (default ``0``; set to ``1`` to enable uvicorn reload)
"""

from __future__ import annotations

import os
import sys


def main() -> None:
    if len(sys.argv) >= 2 and sys.argv[1] == "serve":
        port = os.environ.get("CYCLONE_PORT", "8000")
        reload = os.environ.get("CYCLONE_RELOAD", "0") == "1"
        sys.argv = [
            sys.argv[0],
            "cyclone.api:app",
            "--host", "127.0.0.1",
            "--port", port,
        ]
        if reload:
            sys.argv.append("--reload")
        # DB + EventBus are initialized by the FastAPI lifespan handler
        # (cyclone.api:lifespan); no explicit pre-init is needed here.
        import uvicorn

        uvicorn.main()
        return
    from cyclone.cli import main as cli_main

    cli_main()


if __name__ == "__main__":
    main()
