ARG GO_VERSION=1.26.5
ARG NODE_VERSION=22
ARG GO_BUILD_IMAGE=golang:${GO_VERSION}-alpine
ARG API_RUNTIME_IMAGE=alpine:3.22
ARG NODE_BUILD_IMAGE=node:${NODE_VERSION}-alpine
ARG WEB_RUNTIME_IMAGE=nginx:1.27-alpine

FROM --platform=$BUILDPLATFORM ${GO_BUILD_IMAGE} AS api-builder
ARG TARGETOS=linux
ARG TARGETARCH=amd64
ARG GOPROXY=https://goproxy.cn,direct

ENV GOPROXY=$GOPROXY

RUN apk add --no-cache ca-certificates git
WORKDIR /src

COPY go.work go.work.sum ./
COPY apps/api/go.mod apps/api/go.sum apps/api/
RUN --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    cd apps/api && \
    for attempt in 1 2 3; do \
      go mod download && exit 0; \
      echo "go mod download failed, retrying (${attempt}/3)" >&2; \
      sleep $((attempt * 3)); \
    done; \
    go mod download

COPY apps/api apps/api
RUN --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    cd apps/api && \
    CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -ldflags="-s -w" -o /out/easyai-ai-gateway ./cmd/gateway && \
    CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -ldflags="-s -w" -o /out/easyai-ai-gateway-migrate ./cmd/migrate && \
    CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -ldflags="-s -w" -o /out/easyai-ai-gateway-backfill-binary-results ./cmd/backfill-binary-results && \
    CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -ldflags="-s -w" -o /out/easyai-ai-gateway-acceptance-emulator ./cmd/acceptance-emulator && \
    CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -ldflags="-s -w" -o /out/easyai-ai-gateway-acceptance-callback-collector ./cmd/acceptance-callback-collector && \
    CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -ldflags="-s -w" -o /out/easyai-ai-gateway-acceptance-load ./cmd/acceptance-load && \
    CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -ldflags="-s -w" -o /out/easyai-ai-gateway-acceptance-snapshot ./cmd/acceptance-snapshot && \
    CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -ldflags="-s -w" -o /out/easyai-ai-gateway-acceptance-bootstrap ./cmd/acceptance-bootstrap

FROM ${API_RUNTIME_IMAGE} AS api

RUN apk add --no-cache ca-certificates tzdata wget ffmpeg && \
    adduser -D -H -u 10001 appuser
WORKDIR /app

COPY --from=api-builder /out/easyai-ai-gateway /app/easyai-ai-gateway
COPY --from=api-builder /out/easyai-ai-gateway-migrate /app/easyai-ai-gateway-migrate
COPY --from=api-builder /out/easyai-ai-gateway-backfill-binary-results /app/easyai-ai-gateway-backfill-binary-results
COPY --from=api-builder /out/easyai-ai-gateway-acceptance-emulator /app/easyai-ai-gateway-acceptance-emulator
COPY --from=api-builder /out/easyai-ai-gateway-acceptance-callback-collector /app/easyai-ai-gateway-acceptance-callback-collector
COPY --from=api-builder /out/easyai-ai-gateway-acceptance-load /app/easyai-ai-gateway-acceptance-load
COPY --from=api-builder /out/easyai-ai-gateway-acceptance-snapshot /app/easyai-ai-gateway-acceptance-snapshot
COPY --from=api-builder /out/easyai-ai-gateway-acceptance-bootstrap /app/easyai-ai-gateway-acceptance-bootstrap
COPY apps/api/migrations /app/migrations

RUN mkdir -p /app/data/static/generated /app/data/static/uploaded && \
    chown -R appuser:appuser /app

USER appuser
EXPOSE 8088
ENV APP_ENV=production \
    HTTP_ADDR=:8088 \
    AI_GATEWAY_GENERATED_STORAGE_DIR=/app/data/static/generated \
    AI_GATEWAY_UPLOADED_STORAGE_DIR=/app/data/static/uploaded

CMD ["/app/easyai-ai-gateway"]

FROM --platform=$BUILDPLATFORM ${NODE_BUILD_IMAGE} AS web-builder
ARG NPM_CONFIG_REGISTRY=https://registry.npmjs.org

WORKDIR /src
RUN npm install --registry "$NPM_CONFIG_REGISTRY" -g pnpm@10.18.1

COPY package.json pnpm-lock.yaml pnpm-workspace.yaml nx.json ./
COPY apps/web/package.json apps/web/package.json
COPY packages/contracts/package.json packages/contracts/package.json
RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm/store \
    pnpm install --frozen-lockfile --registry "$NPM_CONFIG_REGISTRY"

COPY packages packages
COPY apps/web apps/web

ARG VITE_GATEWAY_API_BASE_URL=/gateway-api
ARG VITE_BASE_PATH=/
ENV VITE_GATEWAY_API_BASE_URL=$VITE_GATEWAY_API_BASE_URL
ENV VITE_BASE_PATH=$VITE_BASE_PATH
RUN pnpm --filter @easyai-ai-gateway/web build

FROM ${WEB_RUNTIME_IMAGE} AS web

COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=web-builder /src/apps/web/dist /usr/share/nginx/html

EXPOSE 80

FROM alpine:3.22 AS acceptance-netem

RUN apk add --no-cache ca-certificates curl iproute2 socat
COPY scripts/acceptance/netem-entrypoint.sh /usr/local/bin/netem-entrypoint
RUN chmod 0555 /usr/local/bin/netem-entrypoint

USER 65534:65534
ENTRYPOINT ["/usr/local/bin/netem-entrypoint"]
