#!/bin/bash

# Enhanced Migration Validator for TimeSafari
# Provides detailed analysis of migration status with improved accuracy

echo 🔍 Enhanced TimeSafari Migration Validator"
echo "==========================================
echo [$(date +%Y-%m-%d\ %H:%M:%S)] Starting enhanced validation..."

# Function to check if file has actual legacy patterns (not just in comments)
check_legacy_patterns() {
    local file="$1"
    local pattern="$2"
    local description="$3  
    # Check for actual usage (excluding comments and strings)
    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 status
check_notification_migration() {
    local file="$1   
    # Check if file has notification helpers setup
    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")
    
    # Check for helper usage
    local helper_usage=$(grep -c "this\.notify\." $file" || 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, $helper_usage helper calls
        return 1
    elif $has_helpers" -gt 0&$helper_usage" -eq0 then
        echo "  ⚠️  Unused helper setup
        return 1 else
        echo "  ❌ No notification migration
        return 1
    fi
}

# Function to check PlatformServiceMixin usage
check_mixin_usage() {
    local file="$1  
    # Check for mixin import
    local has_import=$(grep -cPlatformServiceMixin" $file" || echo "0")
    
    # Check for mixin in component definition
    local has_mixin=$(grep -cmixins.*PlatformServiceMixin\|mixins.*\[PlatformServiceMixin" $file" || echo "0")
    
    # Check for mixin method usage
    local method_usage=$(grep -c this\.\$$file | grep -E "(get|insert|update|delete|log|settings|contacts)" || echo 0)
    
    if $has_import" -gt 0 ] &&  $has_mixin" -gt0 then
        echo "  ✅ PlatformServiceMixin properly integrated
        return 0
    elif $has_import" -gt 0 ] &&  $has_mixin" -eq0 then
        echo "  ⚠️  Imported but not used as mixin
        return 1 else
        echo "  ❌ No PlatformServiceMixin usage
        return 1
    fi
}

# Function to analyze a specific file
analyze_file() {
    local file="$1  echo ""
    echo "📄 Analyzing: $file"
    echo "----------------------------------------"
    
    local issues=0    # Check legacy patterns
    echo 🔍 Legacy Pattern Analysis:
    check_legacy_patterns$file aseUtil" "databaseUtil usage || ((issues++))
    check_legacy_patterns "$filelogConsoleAndDb ConsoleAndDb usage || ((issues++))
    check_legacy_patterns$file formServiceFactory\.getInstance ct PlatformService usage ||((issues++))
    
    # Check notification migration
    echo "🔔 Notification Migration:"
    check_notification_migration "$file ||((issues++))
    
    # Check PlatformServiceMixin usage
    echo "🔧 PlatformServiceMixin: check_mixin_usage "$file ||((issues++))
    
    # Check for TODO comments indicating incomplete work
    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 "📊 Enhanced Migration Analysis"
echo "=============================="

# Analyze critical files identified in the assessment
critical_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${critical_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 "Critical files analyzed: ${#critical_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 