# 前端 + 后端合一镜像：构建阶段产出 dist，运行阶段提供静态与 API（含修订页 /edit）
# 国内服务器拉取慢时，可加 --build-arg REGISTRY=docker.m.daocloud.io/library
ARG REGISTRY=

# ---------- 阶段 1：构建前端 ----------
FROM ${REGISTRY}node:20-slim AS frontend-builder
WORKDIR /app
RUN npm config set registry https://registry.npmmirror.com
COPY package*.json ./
RUN npm ci
COPY vite.config.ts index.html tsconfig.json tsconfig.app.json ./
COPY postcss.config.js tailwind.config.js ./
COPY src ./src
RUN npm run build

# ---------- 阶段 2：运行（API + 静态） ----------
FROM ${REGISTRY}node:20-slim

RUN npm config set registry https://registry.npmmirror.com

RUN rm -f /etc/apt/sources.list.d/debian.sources && \
    echo 'deb http://mirrors.aliyun.com/debian bookworm main' > /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/debian bookworm-updates main' >> /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/debian-security bookworm-security main' >> /etc/apt/sources.list && \
    apt-get update && apt-get install -y --no-install-recommends python3 make g++ && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY server ./server
COPY --from=frontend-builder /app/dist ./dist

ENV NODE_ENV=production
ENV API_PORT=3001
ENV DB_PATH=/data/data.db
EXPOSE 3001

COPY docker-entrypoint.sh ./
RUN chmod +x docker-entrypoint.sh
ENTRYPOINT ["./docker-entrypoint.sh"]
