FROM python:3.11-slim

ENV PYTHONUNBUFFERED=1

# Install build deps and netcat for the DB-wait
RUN apt-get update && apt-get install -y \
    gcc \
    default-libmysqlclient-dev \
    pkg-config \
    netcat-openbsd \
    curl \
  && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY requirements.txt .
RUN pip install --upgrade pip \
 && pip install -r requirements.txt

COPY . .

# Create a non-root user and give it ownership of /app
RUN useradd -ms /bin/bash appuser \
 && chown -R appuser:appuser /app

# Switch to appuser for everything below
USER appuser

# Prepare entrypoint
COPY --chown=appuser:appuser entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
CMD ["flask", "run", "--host=0.0.0.0"]
