# Base stage for building the static files
FROM node:22-slim AS base
WORKDIR /app

# Install pnpm
RUN corepack enable && corepack prepare pnpm@latest --activate

# Copy package files
COPY package.json pnpm-lock.yaml ./

# Install dependencies
RUN pnpm install --frozen-lockfile

# Copy all source files (respecting .dockerignore)
COPY . .

# Build the application
# Note: The build script includes: astro check && astro build && pagefind --site dist && cp -r dist/pagefind public/
RUN pnpm run build

# Runtime stage for serving the application
FROM nginx:mainline-alpine-slim AS runtime

# Copy the built site from dist directory
COPY --from=base /app/dist /usr/share/nginx/html

# The build script copies pagefind to public/, so we need to copy it back to dist for the runtime
# Copy pagefind if it exists in public/
COPY --from=base /app/public/pagefind /usr/share/nginx/html/pagefind

# Expose port 80
EXPOSE 80

# Add healthcheck
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD wget --no-verbose --tries=1 --spider http://localhost/ || exit 1
