#!/usr/bin/env bash
#
# Read-only smoke test against a WordPress/WooCommerce site.
#
#   ./smoke-test.sh [base_url]
#   SC_NETRC=/path/to/ck.netrc ./smoke-test.sh https://corporatekicks.com.au
#
# Checks:
#   1. Site reachable (WP REST root)
#   2. WooCommerce API auth      (only if SC_NETRC points at a ck/cs netrc)
#   3. Our plugin's public config endpoints (present once the plugin is deployed)
#
# GET-only. Never writes. Safe against production.
set -uo pipefail

BASE="${1:-https://corporatekicks.com.au}"
BASE="${BASE%/}"
NETRC="${SC_NETRC:-}"
pass=0; fail=0

hr() { printf '%s\n' "----------------------------------------------------------"; }
code() { # code <url> [--netrc]
	local url="$1"; shift
	curl -sS --max-time 25 -o /dev/null -w '%{http_code}' "$@" "$url" 2>/dev/null || echo "000"
}
check() { # check <label> <actual> <expected>
	if [ "$2" = "$3" ]; then printf '  ✓ %-42s %s\n' "$1" "$2"; pass=$((pass+1));
	else printf '  ✗ %-42s %s (want %s)\n' "$1" "$2" "$3"; fail=$((fail+1)); fi
}

echo "Smoke test: $BASE"; hr

echo "1) WordPress REST root"
check "GET /wp-json/" "$(code "$BASE/wp-json/")" "200"

echo "2) WooCommerce API (auth)"
if [ -n "$NETRC" ] && [ -f "$NETRC" ]; then
	check "GET /wc/v3/products?per_page=1" \
		"$(code "$BASE/wp-json/wc/v3/products?per_page=1&_fields=id" --netrc-file "$NETRC")" "200"
else
	echo "  – skipped (set SC_NETRC=/path/to/ck.netrc to test)"
fi

echo "3) Shoe Customizer endpoints (after deploy)"
check "GET /shoe-customizer/v1/shoes"     "$(code "$BASE/wp-json/shoe-customizer/v1/shoes")"     "200"
check "GET /shoe-customizer/v1/shoe/af1"  "$(code "$BASE/wp-json/shoe-customizer/v1/shoe/af1")"  "200"

hr
echo "Result: $pass passed, $fail failed"
[ "$fail" -eq 0 ]
