ARG PYTHON_VERSION=3.12
FROM python:${PYTHON_VERSION}-slim

ARG ABSTRACTGATEWAY_VERSION
ARG ABSTRACTGATEWAY_INSTALL_MODE=pypi
ARG ABSTRACTGATEWAY_EXTRAS=

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    HOST=0.0.0.0 \
    PORT=8080 \
    ABSTRACTGATEWAY_DATA_DIR=/data \
    ABSTRACTGATEWAY_USER_AUTH=1 \
    ABSTRACTGATEWAY_WORKFLOW_SOURCE=bundle \
    ABSTRACTGATEWAY_STORE_BACKEND=file \
    ABSTRACTGATEWAY_ALLOWED_ORIGINS=http://localhost:*,http://127.0.0.1:*

RUN addgroup --system abstractgateway \
    && adduser --system --ingroup abstractgateway --home /home/abstractgateway abstractgateway \
    && mkdir -p /data /data/flows /workspace \
    && chown -R abstractgateway:abstractgateway /data /workspace

COPY pyproject.toml README.md CHANGELOG.md /tmp/abstractgateway-src/
COPY src /tmp/abstractgateway-src/src
COPY flows/bundles/basic-agent.flow flows/bundles/basic-agent@0.0.1.flow /tmp/abstractgateway-src/flows/bundles/
COPY docker/abstractgateway-server/entrypoint.sh /usr/local/bin/abstractgateway-docker-entrypoint

RUN apt-get update \
    && apt-get install -y --no-install-recommends build-essential libmagic1 \
    && python -m pip install --upgrade pip \
    && if [ "$ABSTRACTGATEWAY_INSTALL_MODE" = "local" ]; then \
        if [ -n "$ABSTRACTGATEWAY_EXTRAS" ]; then \
            python -m pip install "/tmp/abstractgateway-src[${ABSTRACTGATEWAY_EXTRAS}]"; \
        else \
            python -m pip install "/tmp/abstractgateway-src"; \
        fi; \
    else \
        if [ -n "$ABSTRACTGATEWAY_VERSION" ]; then \
            if [ -n "$ABSTRACTGATEWAY_EXTRAS" ]; then \
                python -m pip install "abstractgateway[${ABSTRACTGATEWAY_EXTRAS}]==${ABSTRACTGATEWAY_VERSION}"; \
            else \
                python -m pip install "abstractgateway==${ABSTRACTGATEWAY_VERSION}"; \
            fi; \
        else \
            if [ -n "$ABSTRACTGATEWAY_EXTRAS" ]; then \
                python -m pip install "abstractgateway[${ABSTRACTGATEWAY_EXTRAS}]"; \
            else \
                python -m pip install "abstractgateway"; \
            fi; \
        fi; \
    fi \
    && apt-get purge -y --auto-remove build-essential \
    && chmod +x /usr/local/bin/abstractgateway-docker-entrypoint \
    && rm -rf /var/lib/apt/lists/* /tmp/abstractgateway-src

USER abstractgateway
WORKDIR /home/abstractgateway

EXPOSE 8080
VOLUME ["/data"]

HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
    CMD python -c "import os, urllib.request; urllib.request.urlopen(f'http://127.0.0.1:{os.getenv(\"PORT\", \"8080\")}/api/health', timeout=3).read()" || exit 1

ENTRYPOINT ["abstractgateway-docker-entrypoint"]
CMD ["sh", "-c", "abstractgateway serve --host \"${HOST:-0.0.0.0}\" --port \"${PORT:-8080}\""]
