"""crondag serve — start the scheduler, API, and UI."""
from __future__ import annotations

import click


@click.command()
@click.option("--host", default=None, help="Web server host")
@click.option("--port", default=None, type=int, help="Web server port")
@click.option("--no-web", is_flag=True, help="Disable web server")
def serve(host: str | None, port: int | None, no_web: bool):
    """Start the crondag server (scheduler + API + UI)."""
    import asyncio
    import logging

    from crondag.config import get_settings

    settings = get_settings()
    if host:
        settings.webserver_host = host
    if port:
        settings.webserver_port = port
    if no_web:
        settings.webserver_enabled = False

    logging.basicConfig(
        level=getattr(logging, settings.log_level.upper(), logging.INFO),
        format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
    )

    from crondag.serve import run_server
    asyncio.run(run_server(settings))
