# Arch Linux base image
FROM archlinux:latest

# Use noninteractive frontend
ENV TERM=xterm

# Prepare pacman and install essential packages
RUN pacman -Sy --noconfirm archlinux-keyring && \
    pacman -Syu --noconfirm && \
    pacman -S --noconfirm \
        base-devel \
        git \
        cmake \
        wget \
        curl \
        python \
        python-pip \
        python-virtualenv \
        fuse2 \
        abseil-cpp \
        sagemath \
        graphviz \
        z3


# Create tools workspace
WORKDIR /home/tools

# Download and install the latest MiniZinc bundle
WORKDIR /home/tools/
RUN LATEST_MINIZINC_VERSION=$(curl -s https://api.github.com/repos/MiniZinc/MiniZincIDE/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")') && \
    wget "https://github.com/MiniZinc/MiniZincIDE/releases/download/${LATEST_MINIZINC_VERSION}/MiniZincIDE-${LATEST_MINIZINC_VERSION}-bundle-linux-x86_64.tgz" && \
    mkdir -p /opt/MiniZinc && \
    tar -xzf "MiniZincIDE-${LATEST_MINIZINC_VERSION}-bundle-linux-x86_64.tgz" -C /opt/MiniZinc --strip-components=1 && \
    rm "MiniZincIDE-${LATEST_MINIZINC_VERSION}-bundle-linux-x86_64.tgz"

# Create a wrapper script to call MiniZinc with the correct shared library path
RUN echo -e '#!/bin/bash\nexec env LD_LIBRARY_PATH=/opt/MiniZinc/lib:$LD_LIBRARY_PATH /opt/MiniZinc/bin/minizinc "$@"' > /usr/local/bin/minizinc && \
    chmod +x /usr/local/bin/minizinc

# Clone Autoguess
RUN git clone https://github.com/hadipourh/autoguess
WORKDIR /home/tools/autoguess

# Create Python virtual environment
RUN python -m venv venv

# Install Python packages inside the virtual environment
RUN venv/bin/pip install --upgrade pip setuptools && \
    venv/bin/pip install python-sat[pblib,aiger] && \
    venv/bin/pip install pysmt && \
    venv/bin/pip install cython && \
    yes | venv/bin/python -m pysmt install --z3 && \
    # yes | venv/bin/python -m pysmt install --btor && \ 
    # (Boolector has been archived, and its developers have officially stopped maintaining it.)
    venv/bin/pip install z3-solver && \
    venv/bin/pip install minizinc && \
    venv/bin/pip install gurobipy && \
    venv/bin/pip install graphviz && \
    venv/bin/pip install dot2tex

# Clean up cache
RUN pacman -Scc --noconfirm && rm -rf /var/cache/pacman/pkg/*

# Set default command
CMD ["/bin/bash", "-c", "echo 'Arch Linux-based MiniZinc + OR-Tools container ready.'; cd /home/tools/autoguess && . venv/bin/activate && exec /bin/bash"]
