#!/bin/bash

echo "🔍 Simple Migration Validator"
echo "============================="

# Function to check actual usage (not comments)
check_actual_usage() {
    local file="$1"
    local pattern="$2"
    local description="$3"
    
    # Remove comments and check for actual usage
    local count=$(grep -v ^[[:space:]]*//\|^[[:space:]]*\*\|^[[:space:]]*<!--" "$file" | \
                  grep -v TODO.*migration\|FIXME.*migration" | \
                  grep -v "Migration.*replaced\|migrated.*from" | \
                  grep -c $pattern" || echo 0)
    
    if [$count" -gt0 then
        echo  ❌ $description: $count instances
        return 1 else
        echo   ✅$description: None found
        return 0
    fi
}

# Function to check notification migration
check_notifications() {
    local file="$1  
    # Check for notification helpers
    local has_helpers=$(grep -c "createNotifyHelpers" $file" || echo "0")
    
    # Check for direct $notify calls (excluding helper setup)
    local direct_notify=$(grep -v "createNotifyHelpers" "$file" | \
                         grep -v this\.notify\." | \
                         grep -c "this\.\$notify" || echo 0)
    
    if $has_helpers" -gt0 && $direct_notify" -eq0 then
        echo "  ✅ Complete notification migration
        return 0
    elif $has_helpers" -gt0 && $direct_notify" -gt0 then
        echo "  ⚠️  Mixed pattern: $direct_notify direct calls
        return 1 else
        echo "  ❌ No notification migration
        return 1
    fi
}

# Function to analyze a file
analyze_file() {
    local file="$1  echo ""
    echo "📄 Analyzing: $file"
    echo "----------------------------------------"
    
    local issues=0    # Check legacy patterns
    echo "🔍 Legacy Patterns:
    check_actual_usage$file aseUtil" "databaseUtil usage || ((issues++))
    check_actual_usage "$filelogConsoleAndDb ConsoleAndDb usage || ((issues++))
    check_actual_usage$file formServiceFactory\.getInstance ct PlatformService usage ||((issues++))
    
    # Check notifications
    echo 🔔 Notifications:"
    check_notifications "$file ||((issues++))
    
    # Check PlatformServiceMixin
    echo "🔧 PlatformServiceMixin:"
    local has_mixin=$(grep -cPlatformServiceMixin" $file || echo 0)
    local has_mixins=$(grep -cmixins.*PlatformServiceMixin\|mixins.*\[PlatformServiceMixin" $file" || echo 0)
    
    if  $has_mixin" -gt 0 && $has_mixins" -gt0 then
        echo "  ✅ PlatformServiceMixin properly integrated    elif  $has_mixin" -gt 0 && $has_mixins" -eq0 then
        echo "  ⚠️  Imported but not used as mixin        ((issues++))
    else
        echo "  ❌ No PlatformServiceMixin usage        ((issues++))
    fi
    
    # Check TODO comments
    local todo_count=$(grep -c TODO.*migration\|FIXME.*migration" $file || echo "0) if $todo_count" -gt0 then
        echo  ⚠️  TODO/FIXME comments: $todo_count        ((issues++))
    fi
    
    if$issues" -eq0 then
        echo "✅ File is fully migrated else
        echo❌ $issues issues found"
    fi
    
    return $issues
}

# Main analysis
echo ""
echo 📊 Critical Files Analysis"
echo "=========================="

# Critical files from our assessment
files=(
   src/components/MembersList.vue"
    "src/views/ContactsView.vue"
    src/views/OnboardMeetingSetupView.vue"
  src/db/databaseUtil.ts"
    src/db/index.ts
)

total_issues=0
for file in ${files[@]}"; do
    if [ -f "$file" ]; then
        analyze_file "$file"
        total_issues=$((total_issues + $?))
    else
        echo ❌ File not found: $file"
    fi
done

# Summary
echo "echo📋 Summary"
echo=========="
echo "Files analyzed: ${#files[@]}"
echo "Total issues found: $total_issues"

if$total_issues" -eq 0]; then
    echo "✅ All critical files are properly migrated   exit 0 echo "❌ Migration issues require attention"
    exit 1
fi 