"""TrustMem server entrypoint.

Usage::

    python -m trustmem

Environment variables (via .env or shell):
    HOST              Listen address (default: 0.0.0.0)
    PORT              Listen port (default: 8080)
    TRUSTMEM_API_KEY  API key for Bearer auth (optional, no auth if unset)
    OPENAI_API_KEY    OpenAI API key (required for embeddings/LLM)
"""
import logging
import os

logger = logging.getLogger("trustmem")


def main() -> None:
    import uvicorn

    try:
        from dotenv import load_dotenv
        load_dotenv(override=False)
    except ImportError:
        pass

    host = os.getenv("HOST", "0.0.0.0")
    port = int(os.getenv("PORT", "8080"))
    api_key = os.getenv("TRUSTMEM_API_KEY")

    if not api_key and host in ("0.0.0.0", "::"):
        logger.warning(
            "Starting server on %s:%s WITHOUT authentication. "
            "Set TRUSTMEM_API_KEY to enable Bearer auth for production use.",
            host, port,
        )

    uvicorn.run("trustmem.server:app", host=host, port=port)


if __name__ == "__main__":
    main()
