#!/bin/bash

# TimeSafari Notification Migration Completeness Validator
# Detects components with incomplete notification migrations

echo "🔔 TimeSafari Notification Migration Validator"
echo "=============================================="

# Function to check if file has raw $notify calls
check_raw_notify() {
    local file="$1"
    if [[ ! -f "$file" ]]; then
        return 1
    fi
    
    # Count $notify calls (excluding comments, initialization, and parameter passing)
    local notify_count=$(grep -v "^[[:space:]]*//\|^[[:space:]]*\*" "$file" | grep -v "createNotifyHelpers(this\.\$notify)" | grep -v "this\.\$notify[[:space:]]*," | grep -v "this\.\$notify[[:space:]]*)" | grep -c "this\.\$notify")
    echo "$notify_count"
}

# Function to check if file has notification helpers setup
check_notify_helpers() {
    local file="$1"
    if [[ ! -f "$file" ]]; then
        return 1
    fi
    
    # Check for createNotifyHelpers import and usage
    local has_import=$(grep -c "createNotifyHelpers" "$file")
    local has_property=$(grep -c "notify!:" "$file")
    local has_created=$(grep -c "this.notify = createNotifyHelpers" "$file")
    
    if [[ $has_import -gt 0 && $has_property -gt 0 && $has_created -gt 0 ]]; then
        echo "complete"
    elif [[ $has_import -gt 0 || $has_property -gt 0 || $has_created -gt 0 ]]; then
        echo "partial"
    else
        echo "none"
    fi
}

echo "🔍 Scanning for notification migration completeness..."

# Get all Vue components using PlatformServiceMixin
mixin_components=$(grep -l "PlatformServiceMixin" src/**/*.vue 2>/dev/null | sort)

incomplete_migrations=()
partial_migrations=()
complete_migrations=()

for component in $mixin_components; do
    notify_count=$(check_raw_notify "$component")
    helper_status=$(check_notify_helpers "$component")
    
    if [[ $notify_count -gt 0 ]]; then
        if [[ "$helper_status" == "none" ]]; then
            incomplete_migrations+=("$component ($notify_count \$notify calls, no helpers)")
        elif [[ "$helper_status" == "partial" ]]; then
            partial_migrations+=("$component ($notify_count \$notify calls, partial helpers)")
        else
            incomplete_migrations+=("$component ($notify_count \$notify calls, but has helpers - mixed pattern)")
        fi
    else
        if [[ "$helper_status" == "complete" ]]; then
            complete_migrations+=("$component")
        elif [[ "$helper_status" == "partial" ]]; then
            partial_migrations+=("$component (unused helper setup)")
        else
            complete_migrations+=("$component")
        fi
    fi
done

# Report results
echo ""
echo "📊 Notification Migration Status Report"
echo "======================================="

if [[ ${#incomplete_migrations[@]} -gt 0 ]]; then
    echo "❌ INCOMPLETE NOTIFICATION MIGRATIONS (${#incomplete_migrations[@]} components):"
    for item in "${incomplete_migrations[@]}"; do
        echo "  - $item"
    done
    echo ""
fi

if [[ ${#partial_migrations[@]} -gt 0 ]]; then
    echo "⚠️  PARTIAL NOTIFICATION MIGRATIONS (${#partial_migrations[@]} components):"
    for item in "${partial_migrations[@]}"; do
        echo "  - $item"
    done
    echo ""
fi

if [[ ${#complete_migrations[@]} -gt 0 ]]; then
    echo "✅ COMPLETE NOTIFICATION MIGRATIONS (${#complete_migrations[@]} components):"
    for item in "${complete_migrations[@]}"; do
        echo "  - $item"
    done
    echo ""
fi

# Summary
total_issues=$((${#incomplete_migrations[@]} + ${#partial_migrations[@]}))
total_components=${#mixin_components[@]}

echo "📈 Summary:"
echo "  Total PlatformServiceMixin components: $total_components"
echo "  Complete notification migrations: ${#complete_migrations[@]}"
echo "  Incomplete/partial migrations: $total_issues"

if [[ $total_issues -gt 0 ]]; then
    echo ""
    echo "🚨 ACTION REQUIRED:"
    echo "   $total_issues components need notification migration completion"
    echo "   Follow: docs/migration-templates/COMPLETE_MIGRATION_CHECKLIST.md"
    exit 1
else
    echo ""
    echo "🎉 ALL NOTIFICATION MIGRATIONS COMPLETE!"
    exit 0
fi 