#!/bin/bash
# Host cutover script: switch from monolithic to module-consuming.
#
# Run AFTER:
#   1. ./scripts/migrate-to-module.sh has copied files to goods-editor-module/
#   2. You've cd'd into goods-editor-module/, npm install'd, and npm run build'd
#
# What this does
# ──────────────
# 1. Renames the current monolithic files to *.monolith.* (kept for reference)
# 2. Renames the *.host.* files to their canonical names
# 3. Deletes the module-owned source from src/ (it now lives in the
#    module). The deletion is the actual cutover; until this runs, both
#    the inline copy and the module import would conflict.
#
# Run from the apparel-designer root:
#   bash scripts/cutover-host.sh

set -euo pipefail

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
ROOT_DIR="$( cd "$SCRIPT_DIR/.." &> /dev/null && pwd )"
cd "$ROOT_DIR"

echo "Cutting over apparel-designer to consume goods-editor module..."
echo ""

# Step 1: Backup the current monolithic files.
echo "→ Backing up monolithic files"
[ -f src/App.jsx ] && mv src/App.jsx src/App.monolith.jsx
[ -f server.js ] && mv server.js server.monolith.js
[ -f package.json ] && mv package.json package.monolith.json
echo "  src/App.jsx        → src/App.monolith.jsx"
echo "  server.js          → server.monolith.js"
echo "  package.json       → package.monolith.json"
echo ""

# Step 2: Promote the *.host.* files to canonical names.
echo "→ Promoting host files"
[ -f src/App.host.jsx ] && mv src/App.host.jsx src/App.jsx
[ -f server.host.js ] && mv server.host.js server.js
[ -f package.host.json ] && mv package.host.json package.json
echo "  src/App.host.jsx   → src/App.jsx"
echo "  server.host.js     → server.js"
echo "  package.host.json  → package.json"
echo ""

# Step 3: Delete module-owned source.
#
# Per the architecture decisions, the host keeps:
#   • src/constants/shirt.js     — host's shirt color/size catalog
#   • src/constants/templates.js — host's template definitions
#   • src/constants/products.js  — host's product config
#   • src/styles/Header.css      — styles the host's Header.jsx
#   • src/styles/PWAInstall.css  — styles the host's PWAInstall.jsx
#
# These are referenced by host-owned components and the new
# App.host.jsx. We delete the OTHER files but preserve these.

echo "→ Removing module-owned source directories from src/"
DIRS_TO_DELETE=(
  "src/components/canvas"
  "src/components/sidebar"
  "src/components/panels"
  "src/components/editor"
  "src/hooks"
  "src/utils"
  "src/i18n"
  "src/test"
)
for dir in "${DIRS_TO_DELETE[@]}"; do
  if [ -d "$dir" ]; then
    rm -rf "$dir"
    echo "  rm -rf $dir"
  fi
done

# Module-owned constants files (delete individually). The host
# keeps shirt.js, templates.js, and products.js — those carry
# data App.host.jsx imports.
MODULE_OWNED_CONSTANTS=(
  "src/constants/fonts.js"
  "src/constants/stickers.js"
  "src/constants/elements.js"
  "src/constants/imageFilters.js"
  "src/constants/textMetrics.js"
  "src/constants/transformer.js"
)
for file in "${MODULE_OWNED_CONSTANTS[@]}"; do
  if [ -f "$file" ]; then
    rm "$file"
    echo "  rm $file"
  fi
done

# Module-owned CSS files (delete individually). The host keeps
# Header.css and PWAInstall.css — those style host-owned components.
MODULE_OWNED_CSS=(
  "src/styles/CanvasHint.css"
  "src/styles/DesignCanvas.css"
  "src/styles/ElementToolbar.css"
  "src/styles/LayersPanel.css"
  "src/styles/MobileBottomSheet.css"
  "src/styles/ModelSilhouette.css"
  "src/styles/PhotoPreEditor.css"
  "src/styles/PreviewModal.css"
  "src/styles/PropertiesPanel.css"
  "src/styles/ShirtOptionsPanel.css"
  "src/styles/Sidebar.css"
  "src/styles/StickersTab.css"
  "src/styles/TShirtSVG.css"
  "src/styles/TemplatesTab.css"
  "src/styles/TextTab.css"
  "src/styles/UploadTab.css"
  "src/styles/ZoomControls.css"
  "src/styles/editor-shell.css"
)
for file in "${MODULE_OWNED_CSS[@]}"; do
  if [ -f "$file" ]; then
    rm "$file"
    echo "  rm $file"
  fi
done

FILES_TO_DELETE=(
  "src/components/MobileBottomSheet.jsx"
  "src/App.css"
)
for file in "${FILES_TO_DELETE[@]}"; do
  if [ -f "$file" ]; then
    rm "$file"
    echo "  rm $file"
  fi
done

# E2E directory — tests live in the module now.
if [ -d "e2e" ]; then
  rm -rf e2e
  echo "  rm -rf e2e"
fi
[ -f "playwright.config.js" ] && rm playwright.config.js && echo "  rm playwright.config.js"
[ -f "vitest.config.js" ] && rm vitest.config.js && echo "  rm vitest.config.js"

# Fonts moved to the module too. Note that `public/stickers/`
# stays in place — the host's vite.config.js still has the
# `stickerManifestPlugin` that reads from `public/stickers/`
# and exposes the `virtual:sticker-manifest` import that
# goods-editor/src/constants/stickers.js consumes. Don't delete
# either the public/stickers/ directory OR the plugin from
# vite.config.js.
if [ -d "fonts" ]; then
  rm -rf fonts
  echo "  rm -rf fonts"
fi

echo ""
echo "Cutover complete. Next steps:"
echo "  1. rm -rf node_modules package-lock.json"
echo "  2. npm install"
echo "  3. npm run dev"
echo ""
echo "If something's broken, restore the monolith with:"
echo "  mv src/App.monolith.jsx src/App.jsx"
echo "  mv server.monolith.js server.js"
echo "  mv package.monolith.json package.json"
echo "  # (Then npm install to restore old deps; src/ won't have the"
echo "  # module-owned files anymore, but you can restore those from"
echo "  # the goods-editor-module/ copy or from git.)"
