#!/bin/bash

# Advanced TypeScript validation for Trip With Us

echo "=== TypeScript Validation ==="

# Configuration
MAX_ERRORS=0
MAX_WARNINGS=5
STRICT_MODE=true

# Run TypeScript compilation
echo "🔍 Running TypeScript compilation check..."
tsc_output=$(cd ../.. && npx tsc --noEmit --pretty 2>&1)
tsc_exit_code=$?

if [ $tsc_exit_code -eq 0 ]; then
    echo "✅ TypeScript compilation successful"
    
    # Check for strict mode compliance
    if [ "$STRICT_MODE" = true ]; then
        echo "🔍 Checking strict mode compliance..."
        
        # Check for any usage
        any_usage=$(grep -r ": any" ../../src/ --include="*.ts" --include="*.tsx" | wc -l)
        if [ "$any_usage" -gt 0 ]; then
            echo "⚠️  Found $any_usage usages of 'any' type"
            echo "Consider adding more specific types for better type safety"
        else
            echo "✅ No 'any' types found"
        fi
        
        # Check for non-null assertions
        non_null=$(grep -r "!" ../../src/ --include="*.ts" --include="*.tsx" | grep -v "!=" | grep -v "!==" | wc -l)
        if [ "$non_null" -gt 10 ]; then
            echo "⚠️  Found $non_null non-null assertions (!)"
            echo "Consider adding proper null checks instead"
        else
            echo "✅ Reasonable use of non-null assertions"
        fi
    fi
    
    # Check for Trip With Us specific patterns
    echo "🔍 Checking Trip With Us TypeScript patterns..."
    
    # Check for proper component prop interfaces
    component_files=$(find ../../src/components -name "*.tsx" 2>/dev/null | wc -l)
    interface_files=$(grep -r "interface.*Props" ../../src/components --include="*.tsx" | wc -l)
    
    if [ "$component_files" -gt 0 ]; then
        interface_ratio=$((interface_files * 100 / component_files))
        if [ "$interface_ratio" -ge 80 ]; then
            echo "✅ $interface_ratio% of components have proper prop interfaces"
        else
            echo "⚠️  Only $interface_ratio% of components have prop interfaces"
        fi
    fi
    
    # Check for Supabase type usage
    supabase_imports=$(grep -r "from.*types/supabase" ../../src/ --include="*.ts" --include="*.tsx" | wc -l)
    if [ "$supabase_imports" -gt 0 ]; then
        echo "✅ Found $supabase_imports files using Supabase types"
    else
        echo "⚠️  No Supabase type imports found"
    fi
    
else
    echo "❌ TypeScript compilation failed"
    
    # Parse and categorize errors
    error_count=$(echo "$tsc_output" | grep -c "error TS" || echo "0")
    
    echo "📊 Compilation Results:"
    echo "  Errors: $error_count"
    
    if [ "$error_count" -gt "$MAX_ERRORS" ]; then
        echo "❌ Too many errors ($error_count > $MAX_ERRORS)"
        
        # Show first 10 errors
        echo ""
        echo "🔴 First 10 TypeScript errors:"
        echo "$tsc_output" | grep "error TS" | head -10
        
        exit 1
    fi
fi

# Create state directory if it doesn't exist
mkdir -p ../state

# Generate TypeScript report
cat > ../state/typescript-report.json << EOF
{
  "timestamp": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
  "compilation_success": $([ $tsc_exit_code -eq 0 ] && echo "true" || echo "false"),
  "error_count": ${error_count:-0},
  "any_usage_count": ${any_usage:-0},
  "non_null_assertions": ${non_null:-0},
  "component_interface_ratio": ${interface_ratio:-0},
  "supabase_type_usage": ${supabase_imports:-0},
  "strict_mode_enabled": $STRICT_MODE
}
EOF

echo "📄 TypeScript report saved to meta-agent/state/typescript-report.json"