from __future__ import annotations

import argparse
import logging
import os
from pathlib import Path

import uvicorn

from .app import make_app


def main() -> None:
    parser = argparse.ArgumentParser(prog="cis490-dashboard")
    parser.add_argument(
        "--host",
        default=os.environ.get("CIS490_DASHBOARD_HOST", "127.0.0.1"),
        help="bind address (default 127.0.0.1; Caddy reverse-proxies dashboard.wg here)",
    )
    parser.add_argument(
        "--port", type=int,
        default=int(os.environ.get("CIS490_DASHBOARD_PORT", "8447")),
        help="bind port (default 8447)",
    )
    parser.add_argument(
        "--data-root", type=Path,
        default=Path(os.environ.get("CIS490_DATA_ROOT", "/var/lib/cis490")),
        help="receiver data root that the feeders read from",
    )
    parser.add_argument(
        "--no-feeders", action="store_true",
        help="don't start the on-disk feeders (useful for local dev)",
    )
    args = parser.parse_args()

    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s %(levelname)s %(name)s %(message)s",
    )

    app = make_app(enable_feeders=not args.no_feeders, data_root=args.data_root)
    uvicorn.run(app, host=args.host, port=args.port, log_config=None)


if __name__ == "__main__":
    main()
