#!/usr/bin/env bash
set -euo pipefail

# update-showcases.sh - Run all blog showcase tests and generate animated GIFs
#
# Usage:
#   scripts/update-showcases.sh              # run all showcases
#   scripts/update-showcases.sh multi-cursor # run only matching showcases

FILTER="${1:-blog_showcase_}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
BLOG_DIR="$ROOT_DIR/docs/blog"

cd "$ROOT_DIR"

echo "=== Running showcase tests (filter: $FILTER) ==="
cargo test --package fresh-editor --test e2e_tests "$FILTER" -- --ignored --nocapture

echo ""
echo "=== Generating GIFs ==="
fail=0
for json in $(find "$BLOG_DIR" -name showcase.json | sort); do
    dir="$(dirname "$json")"
    rel="${dir#"$ROOT_DIR/"}"
    if [[ ! -d "$dir/frames" ]]; then
        echo "SKIP $rel (no frames/)"
        continue
    fi
    if "$SCRIPT_DIR/frames-to-gif.sh" "$rel" 2>&1 | tail -1; then
        :
    else
        echo "FAIL $rel"
        fail=1
    fi
done

if [[ $fail -ne 0 ]]; then
    echo "Some GIFs failed to generate."
    exit 1
fi

echo ""
echo "Done. All showcases updated."
