################
# Stage: Build #
################

FROM python:3.13-slim AS build

WORKDIR /app

# Export poetry dependencies to file
RUN pip install --upgrade pip
RUN pip install poetry poetry-plugin-export
COPY poetry.lock pyproject.toml ./
RUN poetry lock
RUN poetry export --without-hashes --format requirements.txt --output /app/requirements.txt

#####################
# Stage: Production #
#####################
FROM python:3.13-slim AS prod

# ENV PYTHONPATH=/app

WORKDIR /app

# Copy requirements from build stage, and install them
COPY --from=build /app/requirements.txt .
RUN pip install --upgrade pip
RUN pip install --no-cache-dir "fastapi[standard]"
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

# Create a non-root user to run the web server
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

# Run server
EXPOSE 8001
# CMD ["gunicorn", "--bind", "0.0.0.0:8001", "main_package.app:app"]
# CMD ["uvicorn", "main_package.app:app", "--reload", "--host", "0.0.0.0", "--port", "8001", "--proxy-headers"]
CMD ["fastapi", "run", "main_package/app.py", "--reload", "--port", "8001", "--proxy-headers"]

