# Local manual-test reverse proxy for `portal-shell`'s production
# build, simulating what the on-prem reverse proxy will do (per
# ADR-0019 §"URL strategy"):
#
#   - GET /            → smart redirect to /{locale}/ based on
#                        Accept-Language (cookie support lands when
#                        the BFF route is in place). Falls back to
#                        `/fr/` since the APF audience is francophone.
#   - GET /fr/<any>    → serve `dist/.../browser/fr/<any>`, with SPA
#                        fallback to `fr/index.html` for deep links.
#   - GET /en/<any>    → mirror for the EN bundle.
#   - everything else  → redirect to `/fr/` (avoids the http-server
#                        directory-listing footgun we hit in the perf
#                        gate while the previous fix landed).
#
# Used by:
#   - `./infra/local/dev.sh up serve-static` (after building the
#     production bundle with `nx build portal-shell -c=production`).
#   - Convenient for testing the locale switcher end-to-end, the
#     accessibility-route redirect, dark-mode preference plumbing,
#     etc. — anything that needs both locale folders served at their
#     own prefix.

{
	auto_https off
	admin off
}

:4200 {
	encode gzip zstd

	log {
		output stdout
		format console
		level INFO
	}

	# Group the locale-routing logic in an explicit `route` block so
	# directive ordering follows source order, not Caddy's default
	# directive priority — handle / handle_path / redir would
	# otherwise interleave in surprising ways.
	route {
		# Smart redirect for the bare root.
		@root path /
		handle @root {
			@prefers_en header Accept-Language en*
			redir @prefers_en /en/ 302
			redir * /fr/ 302
		}

		# French bundle. `handle_path` strips `/fr` before passing the
		# request through, so `try_files` looks at `<path>` relative
		# to the bundle root (`/srv/dist/fr/`). Missing files fall
		# back to `index.html`, exactly like a production deep-link
		# (e.g. /fr/accessibility) would do via a real proxy.
		handle_path /fr/* {
			root * /srv/dist/fr
			try_files {path} /index.html
			file_server
		}

		# English bundle, mirror of the above.
		handle_path /en/* {
			root * /srv/dist/en
			try_files {path} /index.html
			file_server
		}

		# Anything that didn't match a locale prefix falls back to FR
		# home — same defaulting as the bare root above. Keeps
		# typo'd URLs from landing on a 404.
		redir * /fr/ 302
	}
}
