FROM mambaorg/micromamba:latest

WORKDIR /app
USER root

# Install system dependencies
RUN apt-get update && apt-get install -y \
    curl \
    build-essential \
    libstdc++6 \
    libc6-dev \
    git \
    dos2unix \
    && rm -rf /var/lib/apt/lists/*

# Create appuser and set permissions
RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app

# Install Python 3.12 first
RUN micromamba install -y -n base -c conda-forge python=3.12
RUN micromamba clean --all --yes

USER appuser

COPY --chown=appuser:appuser ../../src ./src/
COPY --chown=appuser:appuser ../../examples ./examples
COPY --chown=appuser:appuser ../../applications ./applications
COPY --chown=appuser:appuser ../../tests ./tests


RUN cat > simstack.toml <<'EOF'
[parameters]
# these are parameters for one user for all hosts
[parameters.common]
resources = ["local", "tests", "int-nano", "horeka", "justus", "self", "ww", "uploads"]
database = "wolfgang_data"
test_database = "wolfgang_test"
connection_string="mongodb://wolfgang:PUTPASSHERE@covalent.int.kit.edu:27017/"
#database = "ui_testing_data"
#test_database = "ui_testing_test"
#connection_string="mongodb://ui_testing:PUTPASSHERE2@covalent.int.kit.edu:27017/"
#database="jia_data"
#test_database="jia_test"
#connection_string="mongodb://jia:PUTPASSHERE3@covalent.int.kit.edu:27017/"
#database = "samira_data"
#test_database = "samira_test"
#connection_string="mongodb://samira:PUTPASSHERE3@covalent.int.kit.edu:27017/"
# these parameters must be adapted for each host
[parameters.self]
ssh-key = "C:\\Users\\bj7610\\Documents\\etc\\.ssh\\surface11_openssh"  # path to your private key
resource = "local" # resource the runner on your computer will use
workdir = "/home/appuser/simstack" # path to your simstack working directory
python_path = [ "C:\\Users\\wolfg\\PyCharmProjects\\simstack-model",
               "C:\\Users\\wolfg\\PyCharmProjects\\simstack-model\\src"]
environment_start = "C:\\Users\\wolfg\\PyCharmProjects\\simstack-model\\.venv\\Scripts\\activate.bat"
[parameters.local]
ssh-key = "C:\\Users\\wolfg\\Documents\\etc\\.ssh\\surface11_openssh"  # path to your private key
resource = "local" # resource the runner on your computer will use
git = ["/app"]
workdir = "/app/workdir" # path to your simstack working directory
python_path = [ ".","./src"]
environment_start = "micromamba activate base"
[parameters.ww]
ssh-key = "C:\\Users\\wolfg\\Documents\\etc\\.ssh\\surface11_openssh"  # path to your private key
resource = "local" # resource the runner on your computer will use
git = ["C:\\Users\\wolfg\\PyCharmProjects\\simstack-model"]
workdir = "C:\\Users\\wolfg\\simstack" # path to your simstack working directory
python_path = [ "C:\\Users\\wolfg\\PyCharmProjects\\simstack-model",
               "C:\\Users\\wolfg\\PyCharmProjects\\simstack-model\\src"]
environment_start = "C:\\Users\\wolfg\\PyCharmProjects\\simstack-model\\.venv\\Scripts\\activate.bat"
[parameters.tests]
ssh-key = "C:\\Users\\wolfg\\Documents\\etc\\.ssh\\surface11_openssh"  # path to your private key
resource = "tests" # resource the runner on your computer will use
workdir = "C:\\Users\\wolfg\\simstack" # path to your simstack working directory
python_path = [ "C:\\Users\\wolfg\\PyCharmProjects\\simstack-model",
               "C:\\Users\\wolfg\\PyCharmProjects\\simstack-model\\src"]
environment_start = "C:\\Users\\wolfg\\PyCharmProjects\\simstack-model\\.venv\\Scripts\\activate.bat"
[parameters.uploads]
ssh-key = "C:\\Users\\wolfg\\Documents\\etc\\.ssh\\surface11_openssh"  # path to your private key
resource = "self" # resource the runner on your computer will used
workdir = "C:\\Users\\bj7610\\simstack" # path to your simstack working directory
python_path = [ "C:\\Users\\wolfg\\PyCharmProjects\\simstack-model",
               "C:\\Users\\wolfg\\PyCharmProjects\\simstack-model\\src"]
[parameters.int-nano]
ssh-key = "/home/ws/bj7610/.ssh/id_rsa"  # path to your private key
workdir = "/home/ws/bj7610/simstack" # path to your simstack working directory
python_path = [ "/home/ws/bj7610/projects/simstack-model",
               "/home/ws/bj7610/projects/simstack-model/src"]
environment_start = "conda activate simstack-env"
# normal users do not have to change anythign below this line
# these are the parameters for the database server

[hosts]
local = "localhost"
int-nano="int-nano.int.kit.edu"
justus="justus.int.kit.edu"
horeka="horeka.int.kit.edu"

[[routes]]
source = "local"
target = "int-nano"
host = "local"

[[routes]]
source = "int-nano"
target = "local"
host = "local"

[[routes]]
source = "horeka"
target = "local"
host = "horeka"

[[routes]]
source = "local"
target = "horeka"
host = "horeka"

[[routes]]
source = "justus"
target = "local"
host = "justus"

[[routes]]
source = "local"
target = "justus"
host = "justus"

[paths]
# Path configuration for the PathManager.
# Each path entry should have a path and an optional drops value.
# The path is the directory to search for Python files
# The "drops" value is a prefix to drop from module names (for import paths)
models = { path = "src/simstack/models", drops = "src" }
methods = { path = "src/simstack/methods", drops = "src" }
ui_testing = { path = "src/simstack/ui_testing", drops = "src" }
examples = { path = "examples", drops = "", use_pickle = false }
applications = { path = "applications", drops = "", use_pickle = false }
tests = { path = "tests", drops = "", use_pickle = false }
EOF


# Create shell script using cat with here document (ensures proper line endings)
RUN cat > /app/shell.sh <<'EOF'
#!/bin/bash
export PYTHONPATH="/app/src:$PYTHONPATH"
cd /app
echo "=== SimStack Conda Environment ==="
echo "Python version: $(micromamba run -n base python --version)"
echo "Working directory: $(pwd)"
echo "Activating micromamba environment..."
eval "$(micromamba shell hook --shell=bash)"
micromamba activate base
exec bash
EOF

RUN cat > /app/setup.py <<'EOF'
HELLO
EOF

RUN cat > /app/requirements.txt <<'EOF'
# Core project dependencies
pip==25.2

# PyPI dependencies from [tool.pixi.pypi-dependencies]
odmantic>=1.0.2,<2
pydantic>=2.10,<3
python-jose>=3.4.0,<4
python-multipart>=0.0.19,<1
hypercorn>=0.17.3,<1
openbabel-wheel>=3.1.1,<4
contextvars>=2.4,<3
mongomock-motor>=0.0.32,<1

# Main application dependencies (skip conda-only packages)
cloudpickle>=3.1.1,<4
fastapi>=0.115.0,<0.116
coolname>=2.2.0,<3
nest-asyncio>=1.6.0,<2
motor>=3.7.0,<4
passlib>=1.7.4,<2
python-dotenv>=1.1.0,<2
pymongo>=4.11.3,<5
requests>=2.32.3,<3
starlette>=0.46.0,<1

# Test dependencies
pytest>=8
pytest-cov
pytest-asyncio>=0.26.0,<1
mongomock>=4.1.0,<5
pytest-mock>=3.0.0

# Development tools
mypy
ruff
pre-commit>=3.7.1,<4
pre-commit-hooks>=4.6.0,<5
typos>=1.23.1,<2
python-build
cruft

rdkit>=2025.3.5
pymatgen>=2024.5.0
ase>=3.23.0
# Skip these - they're conda-only or problematic:
# conda-build  <- Not available on PyPI
# numpy, pandas, scipy, matplotlib <- Already installed via conda
# pymatgen, ase <- May have dependency issues, already installed via conda
# joblib, networkx, beautifulsoup4, mpmath <- Already installed via conda
EOF

RUN cat > /app/conda.txt <<'EOF'
rdkit>=2025.3.5
pymatgen>=2024.5.0
ase>=3.23.0
EOF

RUN export PATH="/home/appuser/.local/bin:$PATH"

RUN micromamba run -n base pip install --no-cache-dir -r /app/requirements.txt

# Install core scientific packages via micromamba
#RUN micromamba run -n base pip install --no-cache-dir -r /app/conda.txt
#RUN micromamba clean --all --yes


# Create entrypoint script using cat with here document
RUN cat > /app/entrypoint.sh <<'EOF'
#!/bin/bash
set -e
export PYTHONPATH="/app/src:$PYTHONPATH"
cd /app

if [ $# -eq 0 ]; then
    echo "Starting SimStack Node with ID: ${NODE_ID:-default}"
    exec micromamba run -n base python -m simstack.core.run_node --node-id "${NODE_ID:-default}"
else
    exec micromamba run -n base "$@"
fi
EOF



# Make scripts executable and fix line endings
RUN chmod +x /app/shell.sh /app/entrypoint.sh && \
    dos2unix /app/shell.sh /app/entrypoint.sh

# Verify scripts
RUN ls -la /app/*.sh && \
    cat /app/shell.sh /app/entrypoint.sh

ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH="/app/src"
EXPOSE 8000

ENTRYPOINT ["/app/entrypoint.sh"]
