#!/bin/bash
# Migration script to update imports from old packages to new @customer-portal/domain structure

set -e

echo "🔄 Starting import migration..."

# Define target directories
BFF_DIR="apps/bff/src"
PORTAL_DIR="apps/portal/src"

# Backup function (optional, commented out for speed)
# backup_files() {
#   echo "📦 Creating backup..."
#   tar -czf migration-backup-$(date +%Y%m%d-%H%M%S).tar.gz "$BFF_DIR" "$PORTAL_DIR"
# }

# Function to replace imports in a directory
migrate_directory() {
  local dir=$1
  local desc=$2
  
  echo "📝 Migrating $desc..."
  
  # Billing domain
  find "$dir" -type f \( -name "*.ts" -o -name "*.tsx" \) -exec sed -i \
    -e 's|@customer-portal/contracts/billing|@customer-portal/domain/billing|g' \
    -e 's|@customer-portal/schemas/business/billing|@customer-portal/domain/billing|g' \
    -e 's|@customer-portal/integrations-whmcs/mappers/billing|@customer-portal/domain/billing|g' \
    {} +
  
  # Subscriptions domain
  find "$dir" -type f \( -name "*.ts" -o -name "*.tsx" \) -exec sed -i \
    -e 's|@customer-portal/contracts/subscriptions|@customer-portal/domain/subscriptions|g' \
    -e 's|@customer-portal/schemas/business/subscriptions|@customer-portal/domain/subscriptions|g' \
    -e 's|@customer-portal/integrations-whmcs/mappers/subscription|@customer-portal/domain/subscriptions|g' \
    {} +
  
  # Payments domain
  find "$dir" -type f \( -name "*.ts" -o -name "*.tsx" \) -exec sed -i \
    -e 's|@customer-portal/contracts/payments|@customer-portal/domain/payments|g' \
    -e 's|@customer-portal/schemas/business/payments|@customer-portal/domain/payments|g' \
    -e 's|@customer-portal/integrations-whmcs/mappers/payment|@customer-portal/domain/payments|g' \
    {} +
  
  # SIM domain
  find "$dir" -type f \( -name "*.ts" -o -name "*.tsx" \) -exec sed -i \
    -e 's|@customer-portal/contracts/sim|@customer-portal/domain/sim|g' \
    -e 's|@customer-portal/schemas/business/sim|@customer-portal/domain/sim|g' \
    -e 's|@customer-portal/integrations-freebit/mappers/sim|@customer-portal/domain/sim|g' \
    -e 's|@customer-portal/schemas/integrations/freebit/requests|@customer-portal/domain/sim/providers/freebit|g' \
    {} +
  
  # Orders domain
  find "$dir" -type f \( -name "*.ts" -o -name "*.tsx" \) -exec sed -i \
    -e 's|@customer-portal/contracts/orders|@customer-portal/domain/orders|g' \
    -e 's|@customer-portal/schemas/integrations/whmcs/order|@customer-portal/domain/orders|g' \
    -e 's|@customer-portal/integrations-whmcs/mappers/order|@customer-portal/domain/orders|g' \
    {} +
  
  # Catalog domain
  find "$dir" -type f \( -name "*.ts" -o -name "*.tsx" \) -exec sed -i \
    -e 's|@customer-portal/contracts/catalog|@customer-portal/domain/catalog|g' \
    -e 's|@customer-portal/domain/src/contracts/catalog|@customer-portal/domain/catalog|g' \
    {} +
  
  # Common types
  find "$dir" -type f \( -name "*.ts" -o -name "*.tsx" \) -exec sed -i \
    -e 's|@customer-portal/contracts/common|@customer-portal/domain/common|g' \
    -e 's|@customer-portal/domain/src/common|@customer-portal/domain/common|g' \
    {} +
  
  # Legacy domain barrel
  find "$dir" -type f \( -name "*.ts" -o -name "*.tsx" \) -exec sed -i \
    -e 's|from "@customer-portal/domain"|from "@customer-portal/domain/billing"|g' \
    {} +
  
  echo "✅ $desc migration complete"
}

# Run migrations
migrate_directory "$BFF_DIR" "BFF"
migrate_directory "$PORTAL_DIR" "Portal"

echo ""
echo "🎉 Import migration complete!"
echo ""
echo "Next steps:"
echo "1. Review changes: git diff"
echo "2. Fix any compilation errors: pnpm typecheck"
echo "3. Update specific mapper usage (WhmcsBillingMapper.transform... → Providers.Whmcs.transform...)"
echo "4. Run tests: pnpm test"

