#!/bin/bash

# Fix Remaining TypeScript Errors - Fast
# Date: 2025-10-29

set -e

echo "🚀 Fixing TypeScript errors..."

WEB_DIR="apps/web/src"

# ============================================
# 1. Fix SpeechRecognition types
# ============================================
echo "1️⃣  Adding SpeechRecognition type declarations..."

if [ -f "$WEB_DIR/composables/useVoiceCommands.ts" ]; then
  # Add type declaration at the top
  if ! grep -q "interface SpeechRecognition" "$WEB_DIR/composables/useVoiceCommands.ts"; then
    cat > /tmp/voice_header.txt << 'EOF'
// SpeechRecognition API types
interface SpeechRecognition extends EventTarget {
  continuous: boolean
  interimResults: boolean
  lang: string
  start(): void
  stop(): void
  onresult: ((event: any) => void) | null
  onerror: ((event: any) => void) | null
  onend: ((event: any) => void) | null
}

interface Window {
  SpeechRecognition: {
    new(): SpeechRecognition
  }
  webkitSpeechRecognition: {
    new(): SpeechRecognition
  }
}

EOF
    # Prepend to file
    cat /tmp/voice_header.txt "$WEB_DIR/composables/useVoiceCommands.ts" > /tmp/voice_temp.ts
    mv /tmp/voice_temp.ts "$WEB_DIR/composables/useVoiceCommands.ts"
    echo "   ✅ Added SpeechRecognition types"
  fi
fi

# ============================================
# 2. Fix Auth0 redirectPath (use appState)
# ============================================
echo "2️⃣  Fixing Auth0 redirectPath..."

# LoginForm.vue
if [ -f "$WEB_DIR/modules/auth/components/LoginForm.vue" ]; then
  sed -i '' 's/redirectPath:/appState: { target: /g' "$WEB_DIR/modules/auth/components/LoginForm.vue"
  sed -i '' 's/redirectPath: /appState: { target: /g' "$WEB_DIR/modules/auth/components/LoginForm.vue"
  echo "   ✅ Fixed LoginForm.vue"
fi

# useAuth.ts
if [ -f "$WEB_DIR/modules/auth/composables/useAuth.ts" ]; then
  sed -i '' 's/redirectPath:/appState: { target: /g' "$WEB_DIR/modules/auth/composables/useAuth.ts"
  sed -i '' 's/redirectPath: /appState: { target: /g' "$WEB_DIR/modules/auth/composables/useAuth.ts"
  echo "   ✅ Fixed useAuth.ts"
fi

# AuthPage.vue
if [ -f "$WEB_DIR/modules/auth/pages/AuthPage.vue" ]; then
  sed -i '' 's/redirectPath:/appState: { target: /g' "$WEB_DIR/modules/auth/pages/AuthPage.vue"
  sed -i '' 's/redirectPath: /appState: { target: /g' "$WEB_DIR/modules/auth/pages/AuthPage.vue"
  echo "   ✅ Fixed AuthPage.vue"
fi

# authStore.ts
if [ -f "$WEB_DIR/modules/auth/stores/authStore.ts" ]; then
  sed -i '' 's/redirectPath:/appState: { target: /g' "$WEB_DIR/modules/auth/stores/authStore.ts"
  sed -i '' 's/redirectPath: /appState: { target: /g' "$WEB_DIR/modules/auth/stores/authStore.ts"
  echo "   ✅ Fixed authStore.ts"
fi

# ============================================
# 3. Fix main.ts locale type
# ============================================
echo "3️⃣  Fixing main.ts locale type..."

if [ -f "$WEB_DIR/app/main.ts" ]; then
  # Add type assertion for locale
  sed -i '' "s/i18n.global.locale = /i18n.global.locale = /g" "$WEB_DIR/app/main.ts"
  sed -i '' "s/locale = userLang/locale = userLang as 'fr' | 'en'/g" "$WEB_DIR/app/main.ts"
  echo "   ✅ Fixed main.ts"
fi

# ============================================
# 4. Fix NetworkStatus.vue onLine declaration
# ============================================
echo "4️⃣  Fixing NetworkStatus.vue..."

if [ -f "$WEB_DIR/app/components/NetworkStatus.vue" ]; then
  # Change 'const onLine' to 'const isOnLine' to avoid conflict
  sed -i '' 's/const onLine = /const isOnLine = /g' "$WEB_DIR/app/components/NetworkStatus.vue"
  sed -i '' 's/onLine\.value/isOnLine.value/g' "$WEB_DIR/app/components/NetworkStatus.vue"
  sed -i '' 's/ onLine,/ isOnLine,/g' "$WEB_DIR/app/components/NetworkStatus.vue"
  echo "   ✅ Fixed NetworkStatus.vue"
fi

echo ""
echo "✅ Script completed!"
echo ""
echo "Run: pnpm --filter @my-roadtrip/web type-check"
