"""Edge server entrypoint.

Supports running as ``python -m sage.edge.server`` or via the ``sage-edge``
console script.

Boundary design:
- `sage.edge.server` handles process bootstrapping and gateway app loading.
- `sage.edge.app` composes edge health routes and optionally mounts the injected
    gateway app.
"""

from __future__ import annotations

import argparse
import os
from typing import Any

import uvicorn

from sage.common.config.ports import SagePorts
from sage.edge.app import create_app


def _load_llm_gateway_app() -> Any:
    """Load gateway ASGI app from boundary module."""
    module = __import__("sage.llm.gateway.server", fromlist=["app"])
    return module.app


def _parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Run the SAGE edge aggregator")
    parser.add_argument(
        "--host",
        default=os.getenv("SAGE_EDGE_HOST", "0.0.0.0"),
        help="Host interface to bind",
    )
    parser.add_argument(
        "--port",
        type=int,
        default=int(os.getenv("SAGE_EDGE_PORT", SagePorts.EDGE_DEFAULT)),
        help="Port to bind (defaults to SagePorts.EDGE_DEFAULT)",
    )
    parser.add_argument(
        "--llm-prefix",
        type=str,
        default=os.getenv("SAGE_EDGE_LLM_PREFIX"),
        help="Optional path prefix for LLM proxy routes (default: /)",
    )
    parser.add_argument(
        "--no-llm",
        action="store_true",
        help="Start edge shell without proxying to an LLM gateway",
    )
    parser.add_argument(
        "--log-level",
        default=os.getenv("SAGE_EDGE_LOG_LEVEL", "info"),
        help="Uvicorn log level (debug, info, warning, error)",
    )
    return parser.parse_args()


def main() -> None:
    args = _parse_args()

    mount_llm = not args.no_llm
    llm_app = _load_llm_gateway_app() if mount_llm else None

    app = create_app(mount_llm=mount_llm, llm_prefix=args.llm_prefix, llm_app=llm_app)

    uvicorn.run(
        app,
        host=args.host,
        port=args.port,
        log_level=args.log_level,
    )


if __name__ == "__main__":
    main()
