"""Demo to showcase interacting with a stateful model in a web browser."""

import asyncio
import logging
import sys

import vizible
import adaptible


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#                                           Constants.                                            #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
_HOST = "127.0.0.1"
_PORT = 8000
_ADDR = f"{_HOST}:{_PORT}/static/"


async def main():
    """Main program loop, run asychnronously."""
    # server = adaptible.MutableHostedLLM(host=_HOST, port=_PORT)
    server = adaptible.MutableHostedLLM(host=_HOST, port=_PORT)
    try:
        await server.up()
        vizible.green("Successfully launched server and enter eval framework")
        vizible.magenta(f"Navigate to: \nhttp://{_ADDR}")
        while True:
            await asyncio.sleep(1)
    except Exception as e:
        raise ValueError("Serving failed.") from e
    finally:
        await server.down()
        vizible.green("Async server terminated successfully if you can read this!**")
        vizible.green("\n\n\t\t**(assumes that you have the ability to read)")


if __name__ == "__main__":
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
        stream=sys.stdout,
    )
    logger = logging.getLogger(__name__)  # Get a logger for this module
    asyncio.run(main())
