# Multi-stage production Dockerfile for gameno-front (Vue 3 + Vite SPA)

# ------------------------------------------------------------------------------
# Stage 1: Build the Vite static application
# ------------------------------------------------------------------------------
FROM node:22-alpine AS build

WORKDIR /app

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

# Copy application source code
COPY . .

# Build argument for API base URL (can be overridden at build time)
ARG VITE_API_BASE_URL
ENV VITE_API_BASE_URL=${VITE_API_BASE_URL}

# Build static assets to /app/dist
RUN npm run build

# ------------------------------------------------------------------------------
# Stage 2: Production web server serving static files via Nginx
# ------------------------------------------------------------------------------
FROM nginx:1.27-alpine AS production

# Inline Nginx configuration for SPA routing, gzip, caching, and healthcheck
RUN printf 'server {\n\
    listen 80;\n\
    server_name localhost;\n\
    root /usr/share/nginx/html;\n\
    index index.html;\n\
    gzip on;\n\
    gzip_types text/plain text/css text/javascript application/javascript application/json image/svg+xml;\n\
    location ~ ^/(healthz|health)$ {\n\
        access_log off;\n\
        default_type text/plain;\n\
        return 200 "OK";\n\
    }\n\
    location / {\n\
        try_files $uri $uri/ /index.html;\n\
    }\n\
    location ~* \\.(?:css|js|jpg|jpeg|gif|png|ico|svg|woff|woff2|ttf)$ {\n\
        expires 1y;\n\
        access_log off;\n\
        add_header Cache-Control "public, immutable";\n\
    }\n\
}\n' > /etc/nginx/conf.d/default.conf

# Copy compiled static assets from build stage
COPY --from=build /app/dist /usr/share/nginx/html

EXPOSE 80

# Health check endpoint for Docker & Coolify targeting /healthz
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
  CMD wget --no-verbose --tries=1 --spider http://localhost:80/healthz || exit 1

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