# Playable Ads Platform - Production Image
#
# 包含：
#   - Express API + 构建服务 (platform/server)
#   - React 前端静态文件 (platform/client)
#   - 填色模板 + 依赖 (templates/coloring) —— 供构建服务调用 vite build

FROM node:20-alpine

WORKDIR /app

# ---- 安装模板依赖（构建服务需要 vite / vite-plugin-singlefile / typescript） ----
COPY templates/coloring/package.json templates/coloring/package-lock.json ./templates/coloring/
RUN cd templates/coloring && npm ci --production=false

# ---- 安装 server 依赖 ----
COPY platform/server/package.json platform/server/package-lock.json ./platform/server/
RUN cd platform/server && npm ci --production=false

# ---- 复制源码 ----
COPY templates/coloring/ ./templates/coloring/
COPY platform/server/   ./platform/server/
COPY platform/client/dist/ ./platform/client/dist/

# ---- 编译 server TypeScript ----
RUN cd platform/server && npx tsc

# ---- 清理 server devDependencies（减小镜像体积） ----
# 保留 node_modules 用于运行时，templates 需要 vite 所以不能删
RUN cd platform/server && npm prune --production

# ---- 运行时目录 ----
RUN mkdir -p /app/storage/creatives

ENV PORT=3001
ENV NODE_ENV=production

EXPOSE 3001

CMD ["node", "platform/server/dist/index.js"]
