# ───────────────────────────────
# Stage 1 — Base Image
# ───────────────────────────────
FROM python:3.11-slim AS base

# Prevent Python from writing .pyc files and force unbuffered output
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1

WORKDIR /app

# Install system dependencies (Postgres client + build tools)
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libpq-dev \
    curl \
    && rm -rf /var/lib/apt/lists/*

# ───────────────────────────────
# Stage 2 — Install Python dependencies
# ───────────────────────────────
COPY requirements.txt .

RUN apt-get update && apt-get install -y --no-install-recommends \
    gfortran pkg-config libopenblas-dev liblapack-dev \
    && rm -rf /var/lib/apt/lists/*

RUN pip install --only-binary=:all: numpy scipy && \
    pip install --no-cache-dir -r requirements.txt && \
    pip install --no-cache-dir "mem0ai[graph]" psycopg[pool] psycopg2-binary


# ───────────────────────────────
# Stage 3 — Copy application
# ───────────────────────────────
COPY neomem ./neomem

# ───────────────────────────────
# Stage 4 — Runtime configuration
# ───────────────────────────────
ENV HOST=0.0.0.0 \
    PORT=7077

EXPOSE 7077

# ───────────────────────────────
# Stage 5 — Entrypoint
# ───────────────────────────────
CMD ["uvicorn", "neomem.server.main:app", "--host", "0.0.0.0", "--port", "7077", "--no-access-log"]