FROM python:3.11-slim

# Install PostgreSQL binaries — required by pytest-postgresql to spawn ephemeral test databases
RUN apt-get update \
    && apt-get install -y --no-install-recommends postgresql \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app
ENV PYTHONPATH=/app

# Copy requirements first so Docker can cache the install layer
COPY backend/engine/requirements.txt backend/engine/requirements.txt
COPY backend/app/requirements/requirements.txt backend/app/requirements/requirements.txt
COPY test.requirements.txt test.requirements.txt

RUN pip install --no-cache-dir \
    -r backend/engine/requirements.txt \
    -r backend/app/requirements/requirements.txt \
    -r test.requirements.txt

# Copy source
COPY . .

# pg_ctl refuses to run as root — create an unprivileged user
RUN useradd -m testuser && chown -R testuser /app
USER testuser

CMD ["pytest"]
