FROM node:22-slim AS base
ENV ASTRO_TELEMETRY_DISABLED=1
WORKDIR /app

# Install dependencies
FROM base AS deps
COPY package.json package-lock.json* ./
RUN npm ci

# Build static site
FROM deps AS build
COPY . .
RUN npm run build

# Production: serve from Caddy
FROM caddy:2-alpine AS prod
COPY --from=build /app/dist /srv
COPY <<'CADDYFILE' /etc/caddy/Caddyfile
:80 {
	root * /srv
	file_server
	encode gzip

	@hashed path /_astro/*
	header @hashed Cache-Control "public, max-age=31536000, immutable"

	@unhashed not path /_astro/*
	header @unhashed Cache-Control "public, max-age=3600"

	handle_errors {
		@404 expression {err.status_code} == 404
		handle @404 {
			rewrite * /404.html
			file_server
		}
	}

	try_files {path} {path}/ /index.html
}
CADDYFILE

EXPOSE 80

# Development: Node with HMR
FROM deps AS dev
ENV ASTRO_TELEMETRY_DISABLED=1
COPY . .
EXPOSE 4321
CMD ["npm", "run", "dev", "--", "--host", "0.0.0.0"]
