FROM python:3.11-slim

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
    gcc \
    libpq-dev \
    curl \
    git \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY requirements.txt /app/
RUN pip install --upgrade pip \
 && pip install -r requirements.txt

# Copy full project
COPY . /app/

# Copy startup script
COPY container_startup.sh /app/
RUN chmod +x /app/container_startup.sh

# Collect static files for WhiteNoise (skip during build if DB not available)
# Will be run during container startup if needed
RUN python manage.py collectstatic --noinput || echo "Skipping collectstatic during build"

# Set default Django settings module (can be overridden via docker-compose)
ENV DJANGO_SETTINGS_MODULE=igny8_core.settings

# Expose port for Gunicorn (matches Portainer docker-compose config)
EXPOSE 8010

# Use startup script as entrypoint to log container lifecycle
# Start using Gunicorn (matches Portainer docker-compose config)
ENTRYPOINT ["/app/container_startup.sh"]
CMD ["gunicorn", "igny8_core.wsgi:application", "--bind", "0.0.0.0:8010"]
