#!/usr/bin/env bash
# sync-to-gitlab.sh — copy source from this repo to a target deployment dir.
#
# Usage: sync-to-gitlab.sh <target-dir>
#
# Same logic as scripts/sync-to-flights-front.sh, but takes the target as a
# required argument and emits machine-friendly output (no "next steps" hints).
set -euo pipefail

SRC_DIR="$(cd "$(dirname "$0")/../.." && pwd)"
TARGET_DIR="${1:-}"

if [ -z "$TARGET_DIR" ]; then
  echo "usage: $0 <target-dir>" >&2
  exit 2
fi
if [ ! -d "$TARGET_DIR" ]; then
  echo "fatal: target directory does not exist: $TARGET_DIR" >&2
  exit 1
fi

DEPLOY_ROOT="$(cd "$TARGET_DIR/.." && pwd)"

echo "syncing $SRC_DIR → $TARGET_DIR"

# 1. Clean target (preserve node_modules, .git, .dockerignore)
find "$TARGET_DIR" -mindepth 1 -maxdepth 1 \
  ! -name "node_modules" \
  ! -name ".dockerignore" \
  ! -name ".git" \
  -exec rm -rf {} +

# 2. Copy source files
cp -r "$SRC_DIR/src"                    "$TARGET_DIR/src"
cp -r "$SRC_DIR/tests"                  "$TARGET_DIR/tests"
cp -r "$SRC_DIR/scripts"                "$TARGET_DIR/scripts"
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"
# CLAUDE.md and AGENTS.md are intentionally NOT copied — internal toolchain
# notes that don't belong in the customer's repo.

# Customer-specific Dockerfile (different defaults than ours)
cp    "$SRC_DIR/Dockerfile.react"       "$TARGET_DIR/Dockerfile"

# 3. Copy deployment configs alongside the app
if [ -d "$SRC_DIR/deployment" ]; then
  mkdir -p "$DEPLOY_ROOT/deployment"
  cp -R "$SRC_DIR/deployment/." "$DEPLOY_ROOT/deployment/"
fi

# 4. Cleanup artifacts that shouldn't ship
find "$TARGET_DIR" -maxdepth 1 -name "*.png" -delete 2>/dev/null || true
rm -rf "$TARGET_DIR/src/server/middleware/"*.test.ts 2>/dev/null || true
rm -f "$TARGET_DIR/scripts/sync-to-flights-front.sh" 2>/dev/null || true
rm -rf "$TARGET_DIR/scripts/ci" 2>/dev/null || true     # our pipeline scripts; not for customer
rm -rf "$TARGET_DIR/scripts/phase-0" 2>/dev/null || true
rm -f  "$TARGET_DIR/scripts/screenshot-diff.ts" 2>/dev/null || true
rm -f  "$TARGET_DIR/playwright-angular.config.ts" 2>/dev/null || true
rm -rf "$TARGET_DIR/tests/e2e-angular" 2>/dev/null || true
rm -rf "$TARGET_DIR/tests/parity" 2>/dev/null || true
rm -rf "$TARGET_DIR/tests/ci" 2>/dev/null || true       # our bash unit tests; not for customer
rm -rf "$TARGET_DIR/.gitea" 2>/dev/null || true         # our workflows; not for customer
rm -rf "$TARGET_DIR/docs/superpowers" 2>/dev/null || true

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

echo "ok: sync complete"
