# ── Build context: archit-studio/ ────────────────────────────────────────────
# Run from the archit-studio/ directory:
#   docker build -t archit-studio .
#
# Or use docker-compose (recommended):
#   docker compose up --build
# ─────────────────────────────────────────────────────────────────────────────


# ── Stage 1: build the React frontend ────────────────────────────────────────
FROM node:22-slim AS frontend-builder

WORKDIR /build/frontend
COPY frontend/package*.json ./
RUN npm ci
COPY frontend/ ./
RUN npm run build


# ── Stage 2: Python runtime ───────────────────────────────────────────────────
FROM python:3.11-slim

# System libraries required by transitive dependencies:
#   libgeos-dev  → shapely (geometry engine used by archit-app)
#   libgl1       → some Pillow/image extras
RUN apt-get update && apt-get install -y --no-install-recommends \
        libgeos-dev \
        libgl1 \
    && rm -rf /var/lib/apt/lists/*

# Copy the Python package and install it.
# archit-studio's extras (io, image, pdf, analysis) are defined on archit-app,
# so we install archit-studio first (which pulls in archit-app), then re-install
# archit-app with its extras to get svgwrite, ezdxf, networkx, scipy, pillow, etc.
COPY . /app/archit-studio/
RUN pip install --no-cache-dir "/app/archit-studio" \
    && pip install --no-cache-dir "archit-app[io,image,pdf,analysis]>=0.3.3"

# Drop the built React bundle into the location the server expects
COPY --from=frontend-builder /build/frontend/dist /app/archit-studio/frontend/dist

WORKDIR /app/archit-studio

# Persist session data outside the container
VOLUME ["/app/archit-studio/data/sessions"]

EXPOSE 8000

# Start the FastAPI server with uvicorn
CMD ["uvicorn", "studio.ui.server:app", \
     "--host", "0.0.0.0", "--port", "8000", \
     "--workers", "1"]
