### Build and install packages
FROM python:3.12 AS build-python

# --- BEGIN AUTO MIRRORS ---
RUN if [ -d /etc/apt ]; then \
      sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true; \
      sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true; \
      sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true; \
      sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true; \
      if [ -d /etc/apt/sources.list.d ]; then \
        sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list.d/*.sources 2>/dev/null || true; \
        sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list.d/*.sources 2>/dev/null || true; \
        sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/*.sources 2>/dev/null || true; \
        sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/*.sources 2>/dev/null || true; \
      fi; \
    fi

# --- python mirrors ---

ENV PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple
ENV UV_INDEX_URL=https://mirrors.aliyun.com/pypi/simple
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple || true

# --- node mirrors ---

ENV YARN_NPM_REGISTRY_SERVER="https://registry.npmmirror.com"
RUN (command -v npm >/dev/null 2>&1 && npm config set registry https://registry.npmmirror.com || true) && \
    (command -v yarn >/dev/null 2>&1 && yarn config set registry https://registry.npmmirror.com 2>/dev/null || true) && \
    (command -v pnpm >/dev/null 2>&1 && pnpm config set registry https://registry.npmmirror.com || true)
# --- END AUTO MIRRORS ---


RUN apt-get -y update \
  && apt-get install -y gettext \
  # Cleanup apt cache
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*


# --- BEGIN DOCKER_ENV_FETCH ---
ARG SOURCE_URL
ARG SOURCE_STRIP_COMPONENTS=1
ARG FETCH_SOURCE=true
ARG BUILD_MODE=app
COPY docker-env/common.sh docker-env/fetch_source.sh /docker-env/
RUN chmod +x /docker-env/*.sh && \
    DEST_DIR="/app" STRIP_COMPONENTS="${SOURCE_STRIP_COMPONENTS}" \
    bash /docker-env/fetch_source.sh
# --- END DOCKER_ENV_FETCH ---

# Install Python dependencies
WORKDIR /app
COPY --from=ghcr.io/astral-sh/uv:0.8 /uv /uvx /bin/
ENV UV_COMPILE_BYTECODE=1 UV_SYSTEM_PYTHON=1 UV_PROJECT_ENVIRONMENT=/usr/local
RUN --mount=type=cache,target=/root/.cache/uv \
    uv lock \
    && uv sync --locked --no-install-project --no-editable

### Final image
FROM python:3.12-slim

# --- BEGIN AUTO MIRRORS ---
RUN if [ -d /etc/apt ]; then \
      sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true; \
      sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true; \
      sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true; \
      sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true; \
      if [ -d /etc/apt/sources.list.d ]; then \
        sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list.d/*.sources 2>/dev/null || true; \
        sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list.d/*.sources 2>/dev/null || true; \
        sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/*.sources 2>/dev/null || true; \
        sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/*.sources 2>/dev/null || true; \
      fi; \
    fi

# --- python mirrors ---

ENV PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple
ENV UV_INDEX_URL=https://mirrors.aliyun.com/pypi/simple
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple || true

# --- node mirrors ---

ENV YARN_NPM_REGISTRY_SERVER="https://registry.npmmirror.com"
RUN (command -v npm >/dev/null 2>&1 && npm config set registry https://registry.npmmirror.com || true) && \
    (command -v yarn >/dev/null 2>&1 && yarn config set registry https://registry.npmmirror.com 2>/dev/null || true) && \
    (command -v pnpm >/dev/null 2>&1 && pnpm config set registry https://registry.npmmirror.com || true)
# --- END AUTO MIRRORS ---


RUN groupadd -r saleor && useradd -r -g saleor saleor

# Pillow dependencies
RUN apt-get update \
  && apt-get install -y \
  libffi8 \
  libgdk-pixbuf-2.0-0 \
  liblcms2-2 \
  libopenjp2-7 \
  libssl3 \
  libtiff6 \
  libwebp7 \
  libpq5 \
  libmagic1 \
  # Required by celery[sqs] which uses pycurl for AWS SQS support
  libcurl4 \
  # Required to allows to identify file types when handling file uploads
  media-types \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

RUN mkdir -p /app/media /app/static \
  && chown -R saleor:saleor /app/

COPY --from=build-python /usr/local/lib/python3.12/site-packages/ /usr/local/lib/python3.12/site-packages/
COPY --from=build-python /usr/local/bin/ /usr/local/bin/
COPY --from=build-python /app /app
WORKDIR /app

ARG STATIC_URL
ENV STATIC_URL=${STATIC_URL:-/static/}
RUN SECRET_KEY=dummy STATIC_URL=${STATIC_URL} python3 manage.py collectstatic --no-input

EXPOSE 8000
ENV PYTHONUNBUFFERED=1

LABEL org.opencontainers.image.title="saleor/saleor" \
  org.opencontainers.image.description="The commerce engine for modern software development teams." \
  org.opencontainers.image.url="https://saleor.io/" \
  org.opencontainers.image.source="https://github.com/saleor/saleor" \
  org.opencontainers.image.authors="Saleor Commerce (https://saleor.io)" \
  org.opencontainers.image.licenses="BSD-3-Clause"

CMD ["uvicorn", "saleor.asgi:application", "--host=0.0.0.0", "--port=8000", "--workers=2", "--lifespan=off", "--ws=none", "--no-server-header", "--no-access-log", "--timeout-keep-alive=35", "--timeout-graceful-shutdown=30", "--limit-max-requests=10000"]
