import logging

import typer
import uvicorn

app = typer.Typer()


def setup_logging(level: int):
    # setup logging
    logging.basicConfig(
        level=level,
        format="[%(asctime)s] [%(thread)d] [%(levelname)s] %(message)s",
        filename="app.log",
        filemode="a",
    )
    console_handler = logging.StreamHandler()
    console_handler.setLevel(level)
    formatter = logging.Formatter("[%(asctime)s] [%(thread)d] [%(levelname)s] %(message)s")
    console_handler.setFormatter(formatter)
    logging.getLogger("").addHandler(console_handler)
    logging.debug("logging start...")


@app.command()
def serve(
    host: str = "127.0.0.1",
    port: int = 19527,
    workers: int = 1,
):
    setup_logging(logging.DEBUG)
    uvicorn.run("mem0x.app.serve:app", host=host, port=port, workers=workers, reload=False)


def run():
    app()
