# Build stage
FROM python:3.10-slim AS builder

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    python3-venv \
    && rm -rf /var/lib/apt/lists/*

# Create and activate virtual environment
RUN python3 -m venv /opt/myenv
ENV PATH="/opt/myenv/bin:$PATH"

WORKDIR /app
COPY . /app
COPY ./asyst /app/asyst
COPY requirements.txt .

# Install dependencies
RUN /opt/myenv/bin/pip install -r /app/requirements.txt \
    && /opt/myenv/bin/python3 -m pip install --upgrade setuptools wheel


# Final image
FROM python:3.10-slim

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*

# Copy virtual environment and application from build stage
COPY --from=builder /opt/myenv /opt/myenv
COPY --from=builder /app /app

# Activate virtual environment in the final image
ENV PATH="/opt/myenv/bin:$PATH"

# Set permissions
RUN chown -R www-data:www-data /app/asyst \
    && chmod -R 755 /app/asyst

# Expose port for Flask API
EXPOSE 5000

# Run the application
CMD ["/opt/myenv/bin/python", "/app/api.py"]