FROM python:3.11-slim AS base

# Install Node.js 20 + nginx for building frontend and serving
RUN apt-get update \
    && apt-get install -y curl gnupg nginx \
    && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
    && apt-get install -y nodejs build-essential \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy source code
COPY mengyadriftbottle-backend ./mengyadriftbottle-backend
COPY mengyadriftbottle-frontend ./mengyadriftbottle-frontend

# -------- Build frontend --------
WORKDIR /app/mengyadriftbottle-frontend
RUN npm install \
    && npm run build

# -------- Install backend deps --------
WORKDIR /app/mengyadriftbottle-backend
RUN pip install --no-cache-dir -r requirements.txt \
    && pip install --no-cache-dir gunicorn

# Prepare runtime artifacts
WORKDIR /app
RUN mkdir -p frontend-dist \
    && cp -r /app/mengyadriftbottle-frontend/dist/* /app/frontend-dist/

# Seed data directory (can be overridden via volume)
RUN mkdir -p /app/data \
    && cp /app/mengyadriftbottle-backend/*.json /app/data/

# Copy nginx config and startup script
COPY nginx.conf /etc/nginx/nginx.conf
COPY start.sh /app/start.sh
RUN chmod +x /app/start.sh

EXPOSE 6767

ENV PORT=6767 \
    BACKEND_PORT=5002 \
    DRIFT_BOTTLE_FRONTEND_DIST=/app/frontend-dist \
    DRIFT_BOTTLE_DATA_DIR=/app/data

CMD ["/app/start.sh"]
