# Static site container for mifi Ventures
# Build stage: SvelteKit build + Critters; final stage: serve dist/ via nginx

# Stage 1: Build (SvelteKit + critical CSS inlining → dist/)
FROM node:20-alpine AS builder

WORKDIR /app

# Enable pnpm via Corepack
RUN corepack enable && corepack prepare pnpm@9.15.0 --activate

COPY package.json pnpm-lock.yaml* ./
RUN pnpm install --frozen-lockfile || pnpm install

COPY svelte.config.js vite.config.ts tsconfig.json postcss.config.js ./
COPY src/ ./src/
COPY static/ ./static/
COPY scripts/ ./scripts/

RUN pnpm run build

# Stage 2: Serve
FROM nginx:alpine

# Copy custom nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf

# Copy built site (dist/) from builder
COPY --from=builder /app/dist/ /usr/share/nginx/html/

# Set proper permissions
RUN chown -R nginx:nginx /usr/share/nginx/html && \
    chmod -R 755 /usr/share/nginx/html

# Expose port 80
EXPOSE 80

# Health check (127.0.0.1 to avoid IPv6 localhost when nginx listens on IPv4 only)
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD wget --quiet --tries=1 --spider http://127.0.0.1/ || exit 1

# Run nginx in foreground
CMD ["nginx", "-g", "daemon off;"]
