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

# Sync the React app from this repo to the flights-front deployment repo.
#
# Usage:
#   ./scripts/sync-to-flights-front.sh [target-dir]
#
# Default target: /Users/gnezim/_projects/tims/flights-front/Aeroflot.Flights.Front

SRC_DIR="$(cd "$(dirname "$0")/.." && pwd)"
TARGET_DIR="${1:-/Users/gnezim/_projects/tims/flights-front/Aeroflot.Flights.Front}"

if [ ! -d "$TARGET_DIR" ]; then
  echo "Error: target directory does not exist: $TARGET_DIR"
  exit 1
fi

echo "Syncing: $SRC_DIR → $TARGET_DIR"
echo ""

# ── Step 1: Clean target (preserve node_modules, .git, .dockerignore) ────────

echo "1/5  Cleaning target directory..."
find "$TARGET_DIR" -mindepth 1 -maxdepth 1 \
  ! -name "node_modules" \
  ! -name ".dockerignore" \
  -exec rm -rf {} +

# ── Step 2: Copy source files ────────────────────────────────────────────────

echo "2/5  Copying source files..."

# App source & config
cp -r "$SRC_DIR/src"                    "$TARGET_DIR/src"
cp -r "$SRC_DIR/tests"                  "$TARGET_DIR/tests"
cp -r "$SRC_DIR/scripts"                "$TARGET_DIR/scripts"
# Modern.js publicDir — fonts, images, leaflet icons, favicons. Copied to
# dist/standalone/public/ at build time. Without these the app serves the
# SPA index for /assets/** and breaks @font-face, img references, tile icons.
cp -r "$SRC_DIR/config"                 "$TARGET_DIR/config"
cp    "$SRC_DIR/package.json"           "$TARGET_DIR/package.json"
cp    "$SRC_DIR/pnpm-lock.yaml"         "$TARGET_DIR/pnpm-lock.yaml"
cp    "$SRC_DIR/tsconfig.json"          "$TARGET_DIR/tsconfig.json"
cp    "$SRC_DIR/modern.config.ts"       "$TARGET_DIR/modern.config.ts"
cp    "$SRC_DIR/module-federation.config.ts" "$TARGET_DIR/module-federation.config.ts"
cp    "$SRC_DIR/vitest.config.ts"       "$TARGET_DIR/vitest.config.ts"
cp    "$SRC_DIR/playwright.config.ts"   "$TARGET_DIR/playwright.config.ts"
cp    "$SRC_DIR/eslint.config.js"       "$TARGET_DIR/eslint.config.js"
cp    "$SRC_DIR/Makefile"               "$TARGET_DIR/Makefile"
cp    "$SRC_DIR/CLAUDE.md"              "$TARGET_DIR/CLAUDE.md"
cp    "$SRC_DIR/AGENTS.md"              "$TARGET_DIR/AGENTS.md"

# Dockerfile — use the SSR-specific one
cp    "$SRC_DIR/Dockerfile.react"       "$TARGET_DIR/Dockerfile"

# ── Step 3: Clean up artifacts that shouldn't be in the target ────────────────

echo "3/5  Cleaning up artifacts..."

# Remove debug screenshots
find "$TARGET_DIR" -maxdepth 1 -name "*.png" -delete 2>/dev/null || true

# Remove Angular-specific files that may have leaked
rm -rf "$TARGET_DIR/src/server/middleware/"*.test.ts 2>/dev/null || true

# Remove this sync script from the target (it's only needed in the source repo)
rm -f "$TARGET_DIR/scripts/sync-to-flights-front.sh" 2>/dev/null || true

# Remove dev-server proxy script (flights-front has its own deployment)
# Keep it — it's useful for local dev

# Remove Angular comparison scripts
rm -rf "$TARGET_DIR/scripts/phase-0" 2>/dev/null || true
rm -f  "$TARGET_DIR/scripts/screenshot-diff.ts" 2>/dev/null || true

# Remove playwright-angular config if copied
rm -f "$TARGET_DIR/playwright-angular.config.ts" 2>/dev/null || true

# Remove test files that reference Angular
rm -rf "$TARGET_DIR/tests/e2e-angular" 2>/dev/null || true
rm -rf "$TARGET_DIR/tests/parity" 2>/dev/null || true

# ── Step 4: Ensure .dockerignore exists ──────────────────────────────────────

echo "4/5  Ensuring .dockerignore..."

if [ ! -f "$TARGET_DIR/.dockerignore" ]; then
  cat > "$TARGET_DIR/.dockerignore" << 'DOCKERIGNORE'
node_modules
dist
coverage
test-results
playwright-report
.playwright-mcp
*.png
*.md
.git
.claude
.gstack
DOCKERIGNORE
fi

# ── Step 5: Summary ─────────────────────────────────────────────────────────

echo "5/5  Done!"
echo ""
echo "Synced files:"
ls -1 "$TARGET_DIR" | grep -v node_modules
echo ""
echo "Next steps:"
echo "  cd $TARGET_DIR"
echo "  pnpm install    # if lock file changed"
echo "  make check      # typecheck + lint + test"
echo "  git diff        # review changes"
