FROM python:3.8-slim

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

# Set working directory
WORKDIR /app

# Create project structure
RUN mkdir -p /app/data /app/results /app/tests /app/docs /app/utils

# Set permissions
RUN chmod -R 777 /app/results

# Copy requirements first to leverage Docker cache
COPY requirements.txt .

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy utility modules first
COPY utils/ ./utils/

# Copy project files
COPY PANARCH.py .
COPY ABM.py .
COPY "Sensitivity Analysis.py" .
COPY tests/ ./tests/

# Copy data files
COPY data/*.graphml ./data/

# Set environment variables
ENV PYTHONPATH=/app
ENV PYTHONUNBUFFERED=1
ENV MPLBACKEND=Agg

# Verify environment and data files
RUN python -c "from utils.utils import setup_directories, get_file_path; \
    setup_directories(); \
    test_file = get_file_path('ventana1-puntas.graphml', ['data']); \
    print(f'Test file found: {test_file}')"

# Default command
CMD ["python", "PANARCH.py"]