# syntax=docker/dockerfile:1.7
FROM --platform=linux/amd64 node:22-alpine AS build
WORKDIR /workspace

COPY package.json ./package.json
COPY yarn.lock ./yarn.lock
# Workspace manifests must exist before yarn install so devDeps (e.g. vue-tsc) are installed
COPY tenant-platform/package.json ./tenant-platform/package.json
COPY docs-site/package.json ./docs-site/package.json

ENV YARN_CACHE_FOLDER=/var/cache/yarn

RUN --mount=type=cache,target=/var/cache/yarn,sharing=locked \
    yarn install --frozen-lockfile

# GIT_COMMIT invalidates source-code layers on every commit, without re-running yarn install
ARG GIT_COMMIT=unknown
COPY tenant-platform ./tenant-platform
COPY docs-site ./docs-site

ARG TENANT_APP_BASE=/
ARG TENANT_API_BASE_URL=/api

RUN cd tenant-platform && \
    VITE_APP_BASE=${TENANT_APP_BASE} \
    VITE_API_BASE_URL=${TENANT_API_BASE_URL} \
    yarn build

RUN cd docs-site && yarn build

FROM --platform=linux/amd64 nginx:1.27-alpine

ARG SERVICE_VERSION=0.0.0
# RUN 写文件确保 SERVICE_VERSION 变化时 BuildKit inline cache 失效（LABEL 单独不可靠）
RUN echo "${SERVICE_VERSION}" > /etc/service_version
LABEL com.xuqm.version="${SERVICE_VERSION}"
LABEL com.xuqm.service="tenant-web"

COPY nginx/tenant.conf /etc/nginx/conf.d/default.conf
COPY --from=build /workspace/tenant-platform/dist /usr/share/nginx/html/tenant
COPY --from=build /workspace/docs-site/docs/.vitepress/dist /usr/share/nginx/html/docs

EXPOSE 80
