FROM python:3.12-slim AS base

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1

WORKDIR /app

COPY pyproject.toml README.md ./
COPY app ./app
COPY tests ./tests
COPY wsgi.py ./

RUN useradd --create-home appuser && \
    mkdir -p /app/instance/outputs && \
    chown -R appuser:appuser /app

FROM base AS production

ENV OUTPUT_DIRECTORY=/app/instance/outputs

RUN pip install --no-cache-dir .

USER appuser

EXPOSE 8000

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "2", "--threads", "4", "wsgi:app"]

FROM base AS test

ENV OUTPUT_DIRECTORY=/app/instance/outputs

RUN pip install --no-cache-dir ".[dev]"

USER appuser

CMD ["python", "-m", "pytest"]
