#!/bin/bash

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
E2E_ROOT="$(dirname "$SCRIPT_DIR")"
REPORT_DIR="${1:-$E2E_ROOT/playwright-report}"
DEPLOY_ROOT="${2:-$E2E_ROOT/.vercel-report-deploy}"
OUTPUT_DIR="$DEPLOY_ROOT/.vercel/output"
STATIC_DIR="$OUTPUT_DIR/static"

if [ ! -d "$REPORT_DIR" ]; then
  echo "Playwright report directory not found: $REPORT_DIR" >&2
  exit 1
fi

if [ ! -f "$REPORT_DIR/index.html" ]; then
  echo "Playwright report is missing index.html: $REPORT_DIR/index.html" >&2
  exit 1
fi

rm -rf "$DEPLOY_ROOT"
mkdir -p "$STATIC_DIR"

cp -R "$REPORT_DIR"/. "$STATIC_DIR"/

cat > "$OUTPUT_DIR/config.json" <<'EOF'
{
  "version": 3
}
EOF

echo "Prepared Vercel output in $DEPLOY_ROOT"
