FROM eclipse-temurin:17-jdk

WORKDIR /app

# Install Python and dependencies
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip \
    python3-venv \
    git \
    && rm -rf /var/lib/apt/lists/*

# Set up Python virtual environment
RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# Install Anthropic and tqdm in the virtual environment
RUN pip3 install --no-cache-dir "anthropic>=0.8.1,<0.9.0" tqdm==4.66.2 python-dotenv==1.0.1

# Clone the official Synthea repository directly into /app
RUN git clone https://github.com/synthetichealth/synthea.git /app-temp && \
    cp -r /app-temp/* /app/ && \
    rm -rf /app-temp

# Create directory structure for our custom modules and python scripts
RUN mkdir -p /app/module_generator \
    /app/output \
    /app/src/main/resources/modules 

# First build the project with the official modules - this will compile the Java code
RUN ./gradlew build -x test

# Make the run_synthea script executable
RUN chmod +x /app/run_synthea

# Copy our Python scripts and module generators
COPY module_generator/*.py /app/module_generator/
COPY src/main/resources/modules/* /app/src/main/resources/modules/
COPY src/main/resources/disease/* /app/src/main/resources/
COPY scripts/* /app/scripts/

# Make scripts executable
RUN chmod +x /app/module_generator/*.py /app/scripts/*

# Verify and fix module JSON structures
RUN python3 /app/scripts/check_condition_structure.py --modules_dir /app/src/main/resources/modules --verbose
# Create a directory for modules that will be mounted from the host
RUN mkdir -p /app/modules

# Test a simple module generation to ensure Synthea works
RUN ./run_synthea -p 1 -m hypertension

# Make directories writable for K8s (may run as non-root)
RUN chmod -R 777 /app/.gradle /app/output /app/build /app/modules \
    /app/src/main/resources/modules

# Set PYTHONPATH to ensure modules can be found
ENV PYTHONPATH="/app"

# Default command when container runs
CMD ["tail", "-f", "/dev/null"]