# Multi-stage build for better security and smaller image size

# Build stage: Install dependencies in a virtual environment
FROM registry.access.redhat.com/ubi10/ubi-minimal:10.1-1778576723 AS builder

# Set working directory
WORKDIR /build

# Install Python 3.12 and build dependencies
RUN microdnf install -y \
    python3.12 \
    python3.12-pip \
    python3.12-devel \
    git \
    gcc \
    gcc-c++ \
    make \
    && microdnf clean all

# Create symlinks for python and pip
RUN ln -sf /usr/bin/python3.12 /usr/bin/python && \
    ln -sf /usr/bin/python3.12 /usr/bin/python3 && \
    ln -sf /usr/bin/pip3.12 /usr/bin/pip && \
    ln -sf /usr/bin/pip3.12 /usr/bin/pip3

# Create virtual environment
RUN python -m venv /opt/venv

# Activate virtual environment
ENV PATH="/opt/venv/bin:$PATH"

# Copy project files needed for installation
COPY pyproject.toml ./
COPY README.md ./
COPY src/ ./src/

# Install ARES and its dependencies in the virtual environment
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir .

# Final stage: Minimal runtime image
FROM registry.access.redhat.com/ubi10/ubi-minimal:10.1-1778576723

# Set environment variables
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PATH="/opt/venv/bin:$PATH"

# Set working directory
WORKDIR /app

# Install only Python runtime (no build tools)
RUN microdnf install -y \
    python3.12 \
    git \
    && microdnf clean all

# Create symlinks for python
RUN ln -sf /usr/bin/python3.12 /usr/bin/python && \
    ln -sf /usr/bin/python3.12 /usr/bin/python3

# Copy virtual environment from builder stage
COPY --from=builder /opt/venv /opt/venv

# Copy application files
COPY gui.py ./
COPY gui_utils.py ./
COPY assets/ ./assets/
COPY example_configs/ ./example_configs/

# Create styles.css if it doesn't exist (referenced in gui.py line 650)
RUN echo "/* Default styles for ARES GUI */" > styles.css

# Create necessary directories and ensure docs/_static exists for favicon
RUN mkdir -p /app/results /app/ares_gui_tempdir /app/docs/source/_static && \
    touch /app/docs/source/_static/favicon.ico

# Create a non-root user for security
RUN useradd -m -u 1000 ares && \
    chown -R ares:ares /app /opt/venv

# Switch to non-root user
USER ares

# Expose the port that NiceGUI uses (default 8081)
EXPOSE 8081

# Set the entrypoint to run the GUI
CMD ["python", "gui.py"]
