# Stage 1: Build Frontend
FROM node:18 AS frontend-builder
WORKDIR /app/frontend
COPY frontend/package*.json ./
RUN npm install
COPY frontend/ ./
# Build the frontend. The output should be in frontend/dist
RUN npm run build

# Stage 2: Backend Runtime
FROM python:3.10-slim

WORKDIR /app

# Install system dependencies required for OpenCV
RUN apt-get update && apt-get install -y \
    libgl1 \
    libglib2.0-0 \
    && rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY backend/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy backend code
COPY backend/ ./backend/
COPY run.py .

# Copy built frontend assets to backend/dist (as expected by main.py)
COPY --from=frontend-builder /app/frontend/dist ./backend/dist

# Create directories for volumes
RUN mkdir -p reports backend/app/static/snapshots

# Expose the port
EXPOSE 8000

# Run the application
CMD ["python", "run.py"]
