"""Web UI commands."""

import click


@click.group()
def webui():
    """Web-based experiment inspector."""


@click.command("start")
@click.option("--host", default="127.0.0.1", help="Bind address.")
@click.option("--port", "-p", default=8000, type=int, help="Port number.")
@click.option("--reload", "do_reload", is_flag=True, default=False, help="Auto-reload on code changes.")
def start_cmd(host: str, port: int, do_reload: bool):
    """Start the ABREKA web UI server."""
    import uvicorn

    from abreka.config import find_abreka_dir
    from abreka.webui.app import create_app

    abreka_dir = find_abreka_dir()
    app = create_app(abreka_dir)

    click.echo(f"Starting ABREKA Web UI at http://{host}:{port}")
    click.echo(f"Project: {abreka_dir.parent.name}")
    click.echo("Press Ctrl+C to stop.\n")

    uvicorn.run(app, host=host, port=port, log_level="info")


webui.add_command(start_cmd, "start")
