ARG BASE_TAG=base-minimal
# ================================
# Stage 1: Build frontend
# ================================
FROM --platform=$BUILDPLATFORM node:20-alpine AS frontend-builder

WORKDIR /app/web

# Copy package files
COPY web/package*.json ./

# Install dependencies
RUN npm ci

# Copy frontend source
COPY web/ ./

# Build frontend
RUN npm run build

# ================================
# Stage 0: Generate build info
# ================================
FROM alpine:3.19 AS build-info

ARG VERSION
ARG BUILD_TIME

# 生成版本信息文件
RUN VERSION_VAL="${VERSION:-dev-$(TZ=Asia/Shanghai date '+%Y%m%d%H%M%S')}" && \
    BUILD_TIME_VAL="${BUILD_TIME:-$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S')}" && \
    mkdir -p /build-info && \
    echo "${VERSION_VAL}" > /build-info/version.txt && \
    echo "${BUILD_TIME_VAL}" > /build-info/build_time.txt

# ================================
# Stage 2: Build backend
# ================================
FROM --platform=$BUILDPLATFORM golang:1.26 AS backend-builder

ARG TARGETOS
ARG TARGETARCH

WORKDIR /app

# Copy build info
COPY --from=build-info /build-info /build-info

# Go mod files
COPY go.mod go.sum ./
RUN go env -w GOPROXY=https://goproxy.cn,direct && go mod download

# Copy backend source
COPY . .

# Copy frontend dist (needed for embedding)
COPY --from=frontend-builder /app/web/dist ./internal/static/dist

# Generate Swagger
RUN go run github.com/swaggo/swag/cmd/swag@latest init -g main.go -o ./openapi_docs

# Build Go binary
RUN VERSION_VAL=$(cat /build-info/version.txt) && \
    BUILD_TIME_VAL=$(cat /build-info/build_time.txt) && \
    CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
    go build -ldflags="-s -w -X github.com/engigu/taskpool/internal/constant.Version=${VERSION_VAL} -X 'github.com/engigu/taskpool/internal/constant.BuildTime=${BUILD_TIME_VAL}'" \
    -o taskpool .

# ================================
# Stage 3: Build Agent (all platforms)
# ================================
FROM --platform=$BUILDPLATFORM golang:1.26 AS agent-builder

WORKDIR /app

# Copy build info
COPY --from=build-info /build-info /build-info

# Go mod files
COPY go.mod go.sum ./
RUN go env -w GOPROXY=https://goproxy.cn,direct && go mod download

# Copy source needed for agent
COPY internal/ ./internal/
COPY agent/ ./agent/

# Build agent for all platforms and package as tar.gz
WORKDIR /app/agent

RUN VERSION_VAL=$(cat /build-info/version.txt) && \
    BUILD_TIME_VAL=$(cat /build-info/build_time.txt) && \
    LDFLAGS="-s -w -X 'main.Version=${VERSION_VAL}' -X 'main.BuildTime=${BUILD_TIME_VAL}'" && \
    mkdir -p /opt/agent && \
    echo "${VERSION_VAL}" > /opt/agent/version.txt && \
    build_agent() { \
        local os=$1; local arch=$2; local suffix=$3; \
        CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -ldflags="${LDFLAGS}" -o taskpool-agent$suffix . && \
        tar -czvf /opt/agent/taskpool-agent-$os-$arch.tar.gz taskpool-agent$suffix config.example.ini && \
        rm taskpool-agent$suffix; \
    } && \
    build_agent linux amd64 "" && \
    build_agent linux arm64 "" && \
    build_agent android arm64 "" && \
    build_agent darwin amd64 "" && \
    build_agent darwin arm64 ""

# ================================
# Stage 4: Final image
# ================================
FROM ghcr.io/engigu/taskpool:${BASE_TAG}

ENV TZ=Asia/Shanghai
ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8
ENV MISE_DATA_DIR=/app/envs/mise
ENV MISE_CONFIG_DIR=/app/envs/mise
ENV PATH="/app/envs/mise/shims:/app/envs/mise/bin:$PATH"

WORKDIR /app

# Copy Go binary
COPY --from=backend-builder /app/taskpool .

# Copy frontend assets
COPY --from=frontend-builder /app/web/dist /www/taskpool

# Copy configs and entrypoint
COPY --from=backend-builder /app/configs ./configs
COPY --from=backend-builder /app/example ./example
COPY builtin/ /www/builtin
COPY docker/docker-entrypoint.minimal.sh ./docker-entrypoint.sh

# Copy agent binaries
COPY --from=agent-builder /opt/agent /opt/agent

COPY docker/mise-hook.sh /etc/profile.d/mise-hook.sh

RUN chmod +x docker-entrypoint.sh \
    && cat /etc/profile.d/mise-hook.sh >> /etc/bash.bashrc \
    && echo "set encoding=utf-8" >> /etc/vim/vimrc \
    && ln -sf /app/taskpool /usr/local/bin/taskpool

ENV MISE_TRUSTED_CONFIG_PATHS=/app/data/scripts

EXPOSE 8052

ENTRYPOINT ["./docker-entrypoint.sh"]
