#!/usr/bin/env bash
# Takes screenshots of a block theme's templates at 1440, 782 and 480px.
#
#   bash .theme-utils/screenshots/capture.sh <theme-slug> <out-dir>
#
# Starts WordPress Playground with the theme and the fixture content in
# fixture/, then captures each template with Playwright (capture.cjs).
# Needs Node 20+ and Playwright: set PLAYWRIGHT_PATH to a playwright package
# folder if `require('playwright')` can't find it, and CHROME_PATH to use a
# browser other than the one Playwright downloaded.
set -euo pipefail

SLUG="${1:?theme slug}"
OUT="${2:?output folder}"
HERE="$(cd "$(dirname "$0")" && pwd)"
REPO="$(cd "$HERE/../.." && pwd)"
PORT="${PORT:-9400}"
PLAYGROUND="${PLAYGROUND_CLI:-npx -y @wp-playground/cli@latest}"

[[ "$SLUG" =~ ^[A-Za-z0-9_-]+$ ]] || { echo "Bad theme slug: $SLUG"; exit 1; }
[ -f "$REPO/$SLUG/templates/index.html" ] || { echo "$SLUG is not a block theme"; exit 1; }
mkdir -p "$OUT"
OUT="$(cd "$OUT" && pwd)"

# Run from a temp folder: npx inside the repo scans the npm workspaces and can
# fail on theme folders it can't read.
WORK="$(mktemp -d)"
trap 'kill "$SERVER" 2>/dev/null || true; rm -rf "$WORK"' EXIT
cd "$WORK"

MOUNTS=(
	"--mount=$REPO/$SLUG:/wordpress/wp-content/themes/$SLUG"
	"--mount=$HERE/fixture:/fixture"
	"--mount=$HERE/fixture/mu-plugins:/wordpress/wp-content/mu-plugins"
)
THEME_DIRS=( "$REPO/$SLUG" )
# A child theme only loads with its parent mounted next to it.
PARENT=$(sed -n 's/^[[:space:]]*Template:[[:space:]]*//p' "$REPO/$SLUG/style.css" | head -1 | tr -d '\r' | xargs || true)
if [ -n "$PARENT" ] && [[ "$PARENT" =~ ^[A-Za-z0-9_-]+$ ]] && [ -d "$REPO/$PARENT" ]; then
	MOUNTS+=( "--mount=$REPO/$PARENT:/wordpress/wp-content/themes/$PARENT" )
	THEME_DIRS+=( "$REPO/$PARENT" )
fi

# A theme with WooCommerce templates gets WooCommerce and its sample products.
STORE=""
for DIR in "${THEME_DIRS[@]}"; do
	if compgen -G "$DIR/templates/*product*.html" > /dev/null || [ -f "$DIR/templates/page-cart.html" ]; then
		STORE='{ "step": "installPlugin", "pluginData": { "resource": "wordpress.org/plugins", "slug": "woocommerce" } },'
	fi
done

cat > blueprint.json <<JSON
{
	"\$schema": "https://playground.wordpress.net/blueprint-schema.json",
	"preferredVersions": { "php": "8.3", "wp": "latest" },
	"steps": [
		$STORE
		{ "step": "activateTheme", "themeFolderName": "$SLUG" },
		{ "step": "runPHP", "code": "<?php require '/wordpress/wp-load.php'; require '/fixture/setup.php';" }
	]
}
JSON

$PLAYGROUND server --port="$PORT" --blueprint=blueprint.json "${MOUNTS[@]}" > server.log 2>&1 &
SERVER=$!

# Playground answers 502 "WordPress is not ready yet", then redirects and 404s
# while the blueprint runs, so wait until setup.php says it has finished, twice
# in a row. The first run downloads WordPress, so give it a few minutes.
ready() { curl -sf "http://127.0.0.1:$PORT/?shot-info" | grep -q '"ready":true'; }
for _ in $(seq 1 300); do
	if ready && sleep 1 && ready; then break; fi
	if ! kill -0 "$SERVER" 2>/dev/null; then cat server.log; echo "Playground stopped"; exit 1; fi
	sleep 1
done
ready || { cat server.log; echo "Playground did not finish setting up"; exit 1; }

node "$HERE/capture.cjs" "http://127.0.0.1:$PORT" "$OUT"
