"""``albus serve`` — start the AlbusOS HTTP server."""

from __future__ import annotations

from typing import Annotated

import typer

from albusos.cli import app
from albusos.cli._helpers import err


@app.command("serve")
def serve_cmd(
    host: Annotated[str, typer.Option("--host", "-h", help="Bind address.")] = "127.0.0.1",
    port: Annotated[int, typer.Option("--port", "-p", help="Bind port.")] = 8000,
    reload: Annotated[bool, typer.Option("--reload", help="Enable auto-reload.")] = False,
    workers: Annotated[int, typer.Option("--workers", "-w", help="Number of workers.")] = 1,
) -> None:
    """Start the AlbusOS FastAPI server (wraps uvicorn)."""
    try:
        import uvicorn
    except ImportError:
        err.print(
            "[red]Error:[/red] uvicorn is not installed.\n"
            "  Install it with: [bold]pip install albusos\\[server][/bold]"
        )
        raise typer.Exit(1) from None

    err.print(f"Starting AlbusOS server on [bold]{host}:{port}[/bold]")
    uvicorn.run(
        "albusos.server:app",
        host=host,
        port=port,
        reload=reload,
        workers=workers,
    )
