# 使用指定基础镜像
FROM docker.1ms.run/node:20.19.4

# 定义构建参数
ARG WX_MINI_APP_ID
ARG WX_MINI_APP_SECRET
ARG WECHAT_MERCHANT_ID
ARG WECHAT_V3_KEY
ARG WECHAT_PAY_NOTIFY_URL
ARG WECHAT_PUBLIC_KEY
ARG WECHAT_PRIVATE_KEY
ARG WECHAT_MERCHANT_CERT_SERIAL_NO
ARG WECHAT_PLATFORM_CERT_SERIAL_NO

# 安装 pnpm
RUN npm install -g pnpm@10.18.3

# 配置国内npm镜像
RUN pnpm config set registry https://registry.npmmirror.com/
RUN pnpm config set @jsr:registry https://npm.jsr.io

# 设置工作目录
WORKDIR /workspace

# 复制根目录配置文件
COPY package.json pnpm-workspace.yaml pnpm-lock.yaml .npmrc ./

# 复制各项目 package.json
COPY web/package.json ./web/
COPY packages/server/package.json ./packages/server/

# 安装依赖
RUN pnpm install --frozen-lockfile

# 复制项目文件
COPY . .

# 设置环境变量
ENV WX_MINI_APP_ID=${WX_MINI_APP_ID}
ENV WX_MINI_APP_SECRET=${WX_MINI_APP_SECRET}
ENV WECHAT_MERCHANT_ID=${WECHAT_MERCHANT_ID}
ENV WECHAT_V3_KEY=${WECHAT_V3_KEY}
ENV WECHAT_PAY_NOTIFY_URL=${WECHAT_PAY_NOTIFY_URL}
ENV WECHAT_PUBLIC_KEY=${WECHAT_PUBLIC_KEY}
ENV WECHAT_PRIVATE_KEY=${WECHAT_PRIVATE_KEY}
ENV WECHAT_MERCHANT_CERT_SERIAL_NO=${WECHAT_MERCHANT_CERT_SERIAL_NO}
ENV WECHAT_PLATFORM_CERT_SERIAL_NO=${WECHAT_PLATFORM_CERT_SERIAL_NO}

# 构建 web 应用
RUN cd web && pnpm run build

# 暴露端口(根据实际需要调整)
EXPOSE 8080

# 启动命令
CMD ["sh", "-c", "cd web && pnpm run start"]