#!/bin/bash
# Migration script: copy files from apparel-designer into goods-editor-module.
#
# Why this exists
# ───────────────
# The module-extraction refactor moves ~80 files from an existing app
# into a new module repo. Most files copy verbatim — no surgery needed.
# Doing this via shell is dramatically faster than reading/writing each
# file through an AI tool.
#
# What this script does
# ─────────────────────
# 1. Copies utils, hooks, constants, i18n, styles, test setup, scripts
# 2. Copies the canvas / sidebar / panels / editor / mobile components
# 3. Copies the fonts/ directory and public/stickers/
# 4. Copies all test files (Vitest *.test.js + e2e/)
# 5. Reports what was moved
#
# What this script does NOT do
# ────────────────────────────
# - It does not copy App.jsx, main.jsx, index.html (host-side files)
# - It does not copy Header.jsx, PWAInstall.jsx, OfflineIndicator.jsx (host-side)
# - It does not copy server.js (refactored separately into the module's
#   server/ directory)
# - It does not run npm install or build anything
# - It does not delete anything from the source — the apparel-designer
#   project is left intact until you've verified the module works.
#
# Run from the parent directory of both repos:
#   cd /Users/khalid/Documents/Claude-Desktop
#   bash apparel-designer/scripts/migrate-to-module.sh

set -euo pipefail

# Resolve paths relative to this script's location so it doesn't matter
# where you invoke it from.
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
SOURCE_DIR="$( cd "$SCRIPT_DIR/.." &> /dev/null && pwd )"
PARENT_DIR="$( cd "$SOURCE_DIR/.." &> /dev/null && pwd )"
DEST_DIR="$PARENT_DIR/goods-editor-module"

if [ ! -d "$DEST_DIR" ]; then
  echo "Error: goods-editor-module not found at $DEST_DIR"
  echo "Expected layout:"
  echo "  $PARENT_DIR/"
  echo "    apparel-designer/    (source, where this script lives)"
  echo "    goods-editor-module/ (destination)"
  exit 1
fi

echo "Source:      $SOURCE_DIR"
echo "Destination: $DEST_DIR"
echo ""

# Helper: copy a directory's contents, preserving structure.
# Usage: copy_dir <source-relative> <dest-relative>
copy_dir() {
  local src="$SOURCE_DIR/$1"
  local dst="$DEST_DIR/$2"
  if [ ! -d "$src" ]; then
    echo "  skip (missing): $1"
    return
  fi
  mkdir -p "$dst"
  cp -R "$src/." "$dst/"
  local count
  count=$(find "$dst" -type f | wc -l | tr -d ' ')
  echo "  $1 → $2 ($count files)"
}

# Helper: copy a single file.
# Usage: copy_file <source-relative> <dest-relative>
copy_file() {
  local src="$SOURCE_DIR/$1"
  local dst="$DEST_DIR/$2"
  if [ ! -f "$src" ]; then
    echo "  skip (missing): $1"
    return
  fi
  mkdir -p "$(dirname "$dst")"
  cp "$src" "$dst"
  echo "  $1 → $2"
}

echo "=== Phase 1: Utilities, hooks, constants, i18n, styles ==="
copy_dir "src/utils" "src/utils"
copy_dir "src/hooks" "src/hooks"
copy_dir "src/constants" "src/constants"
copy_dir "src/i18n" "src/i18n"
copy_dir "src/styles" "src/styles"
copy_dir "src/test" "src/test"
echo ""

echo "=== Phase 2: Canvas / sidebar / panels / editor components ==="
copy_dir "src/components/canvas" "src/components/canvas"
copy_dir "src/components/sidebar" "src/components/sidebar"
copy_dir "src/components/panels" "src/components/panels"
copy_dir "src/components/editor" "src/components/editor"
copy_file "src/components/MobileBottomSheet.jsx" "src/components/MobileBottomSheet.jsx"
echo ""

echo "=== Phase 3: App.css (canvas-area styles, etc.) ==="
# App.css contains styles used by DesignCanvas / the canvas-area frame.
# These are module concerns even though the file is named "App.css".
# We copy it into src/styles/ with a clearer name.
copy_file "src/App.css" "src/styles/editor-shell.css"
echo ""

echo "=== Phase 4: Fonts and the font-fetch script ==="
copy_dir "fonts" "fonts"
if [ -f "$SOURCE_DIR/scripts/fetch-fonts.mjs" ]; then
  mkdir -p "$DEST_DIR/scripts"
  cp "$SOURCE_DIR/scripts/fetch-fonts.mjs" "$DEST_DIR/scripts/fetch-fonts.mjs"
  echo "  scripts/fetch-fonts.mjs → scripts/fetch-fonts.mjs"
fi
echo ""

echo "=== Phase 5: Default sticker library ==="
# The module ships defaults; host can extend/replace via props.
if [ -d "$SOURCE_DIR/public/stickers" ]; then
  mkdir -p "$DEST_DIR/src/assets/stickers"
  cp -R "$SOURCE_DIR/public/stickers/." "$DEST_DIR/src/assets/stickers/"
  count=$(find "$DEST_DIR/src/assets/stickers" -type f | wc -l | tr -d ' ')
  echo "  public/stickers → src/assets/stickers ($count files)"
fi
echo ""

echo "=== Phase 6: Playwright E2E tests ==="
if [ -d "$SOURCE_DIR/e2e" ]; then
  cp -R "$SOURCE_DIR/e2e" "$DEST_DIR/e2e"
  count=$(find "$DEST_DIR/e2e" -type f | wc -l | tr -d ' ')
  echo "  e2e/ → e2e/ ($count files)"
fi
if [ -f "$SOURCE_DIR/playwright.config.js" ]; then
  cp "$SOURCE_DIR/playwright.config.js" "$DEST_DIR/playwright.config.js"
  echo "  playwright.config.js → playwright.config.js"
fi
echo ""

echo "=== Phase 7: ESLint config ==="
if [ -f "$SOURCE_DIR/eslint.config.js" ]; then
  cp "$SOURCE_DIR/eslint.config.js" "$DEST_DIR/eslint.config.js"
  echo "  eslint.config.js → eslint.config.js"
fi
echo ""

echo "=== Done. Summary ==="
echo ""
echo "Files in $DEST_DIR:"
find "$DEST_DIR/src" -type f 2>/dev/null | wc -l | xargs echo "  src/ files:"
find "$DEST_DIR/e2e" -type f 2>/dev/null | wc -l | xargs echo "  e2e/ files:"
find "$DEST_DIR/fonts" -type f 2>/dev/null | wc -l | xargs echo "  fonts/ files:"
echo ""
echo "Next steps:"
echo "  1. cd $DEST_DIR"
echo "  2. npm install"
echo "  3. The surgical-new-files (index.js, ApparelDesigner.jsx, server,"
echo "     dev-host, release script, README) are already in place."
echo "  4. npm run build    # builds the library to dist/"
echo "  5. npm run dev      # runs the example dev host"
echo ""
