# 国内 Jenkins 默认走 DaoCloud 公共镜像代理（完整代理 docker.io/library），避免拉取超时
# 可覆盖: docker build --build-arg NODE_IMAGE=node:20-alpine ...
ARG NODE_IMAGE=docker.m.daocloud.io/library/node:20-alpine
ARG PYTHON_IMAGE=docker.m.daocloud.io/library/python:3.11-slim

# Stage 1: build frontend
FROM ${NODE_IMAGE} AS web-builder

WORKDIR /app/web

COPY web/package.json web/package-lock.json ./
RUN npm config set registry https://registry.npmmirror.com \
    && npm ci

COPY web/ ./
RUN npm run build

# Stage 2: Python runtime
FROM ${PYTHON_IMAGE}

WORKDIR /app

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1

RUN sed -i 's|deb.debian.org|mirrors.aliyun.com|g; s|security.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \
    sed -i 's|deb.debian.org|mirrors.aliyun.com|g; s|security.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list 2>/dev/null || true

RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc \
    && rm -rf /var/lib/apt/lists/*

COPY pyproject.toml requirements.txt README.md ./
COPY supply_agent/ supply_agent/
COPY supply_infra/ supply_infra/
COPY agents/ agents/
COPY api/ api/
COPY jobs/ jobs/

RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com ".[odps]"

COPY --from=web-builder /app/web/dist ./web/dist

EXPOSE 8080

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