# Build arguments for version pinning (matching devbox.json)
ARG NODE_VERSION=24.10.0
ARG GO_VERSION=1.25.1
ARG HUGO_VERSION=0.151.0

# Build stage - use same versions as local devbox environment
FROM node:${NODE_VERSION}-bookworm AS builder

# Get target architecture for multi-platform builds
ARG TARGETARCH

# Install Git (needed for Hugo's enableGitInfo)
RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*

# Install Go (map TARGETARCH: amd64->amd64, arm64->arm64)
ARG GO_VERSION
RUN wget -q https://go.dev/dl/go${GO_VERSION}.linux-${TARGETARCH}.tar.gz && \
    tar -C /usr/local -xzf go${GO_VERSION}.linux-${TARGETARCH}.tar.gz && \
    rm go${GO_VERSION}.linux-${TARGETARCH}.tar.gz

ENV PATH="/usr/local/go/bin:${PATH}"
ENV GOPATH="/go"
ENV PATH="${GOPATH}/bin:${PATH}"

# Install Hugo extended (map TARGETARCH: amd64->amd64, arm64->arm64)
ARG HUGO_VERSION
RUN wget -q https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-${TARGETARCH}.tar.gz && \
    tar -xzf hugo_extended_${HUGO_VERSION}_linux-${TARGETARCH}.tar.gz && \
    mv hugo /usr/local/bin/ && \
    rm hugo_extended_${HUGO_VERSION}_linux-${TARGETARCH}.tar.gz && \
    hugo version

WORKDIR /src

# Copy package files and install npm dependencies
COPY package*.json ./
RUN npm ci

# Copy all source files
COPY . .

# Build Hugo site (Git info wird aus dem aktuellen Kontext genommen, nicht aus .git)
# Hugo sucht nach .git, findet es nicht, und überspringt Git-Info automatisch
RUN hugo --gc --minify

# Runtime stage - nginx to serve static content
FROM nginx:1.27-alpine

# Copy built site from builder
COPY --from=builder /src/public /usr/share/nginx/html

# Copy custom nginx config
RUN echo 'server {' > /etc/nginx/conf.d/default.conf && \
    echo '    listen 80;' >> /etc/nginx/conf.d/default.conf && \
    echo '    server_name _;' >> /etc/nginx/conf.d/default.conf && \
    echo '    root /usr/share/nginx/html;' >> /etc/nginx/conf.d/default.conf && \
    echo '    index index.html;' >> /etc/nginx/conf.d/default.conf && \
    echo '' >> /etc/nginx/conf.d/default.conf && \
    echo '    location / {' >> /etc/nginx/conf.d/default.conf && \
    echo '        try_files $uri $uri/ /index.html;' >> /etc/nginx/conf.d/default.conf && \
    echo '    }' >> /etc/nginx/conf.d/default.conf && \
    echo '' >> /etc/nginx/conf.d/default.conf && \
    echo '    gzip on;' >> /etc/nginx/conf.d/default.conf && \
    echo '    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;' >> /etc/nginx/conf.d/default.conf && \
    echo '}' >> /etc/nginx/conf.d/default.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
