#!/usr/bin/env bash
# Creates lens-ai-visibility-monitor.zip from the plugin folder, EXCLUDING:
#  - .DS_Store / macOS metadata
#  - .git / dev tooling
#  - WordPress.org marketing assets (banners, icons, screenshots — those go in SVN /assets/)
#
# What ships in the zip:
#  - All PHP files
#  - assets/css/ + assets/js/ + assets/vendor/  (loaded by the plugin at runtime)
#  - languages/
#  - readme.txt, LICENSE, uninstall.php, main PHP file
#
# Run from one directory *above* the plugin folder:
#   bash lens-ai-visibility-monitor/build-zip.sh

set -e

PLUGIN_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PLUGIN_NAME="$(basename "$PLUGIN_DIR")"
PARENT_DIR="$(dirname "$PLUGIN_DIR")"
OUT="$PARENT_DIR/$PLUGIN_NAME.zip"

# Strip macOS metadata that Finder re-creates constantly
find "$PLUGIN_DIR" -name '.DS_Store' -delete 2>/dev/null || true
find "$PLUGIN_DIR" -name '._*' -delete 2>/dev/null || true
find "$PLUGIN_DIR" -name 'Thumbs.db' -delete 2>/dev/null || true

cd "$PARENT_DIR"
rm -f "$OUT"

zip -r "$OUT" "$PLUGIN_NAME" \
  -x "*/.DS_Store" \
  -x "*/.git/*" \
  -x "*/.github/*" \
  -x "*/node_modules/*" \
  -x "*/tests/*" \
  -x "*/.idea/*" \
  -x "*/.vscode/*" \
  -x "*/build-zip.sh" \
  -x "*/Archive.zip" \
  -x "*/.distignore" \
  -x "*/assets/banner-*.png" \
  -x "*/assets/banner-*.jpg" \
  -x "*/assets/icon-*.png" \
  -x "*/assets/icon.svg" \
  -x "*/assets/screenshot-*.png" \
  -x "*/assets/screenshot-*.jpg" \
  -x "*/assets/_banner.svg" \
  -x "*/assets/README.md"

echo "Built: $OUT"
ls -lh "$OUT"

# Quick sanity check: confirm marketing assets are NOT in the zip
echo
echo "Sanity check — marketing assets should be ABSENT from the zip:"
unzip -l "$OUT" | grep -E "banner|icon|screenshot" || echo "  ✓ none found (good)"
echo
echo "Sanity check — plugin runtime assets SHOULD be in the zip:"
unzip -l "$OUT" | grep -E "assets/(css|js|vendor)" | head -5
