#!/bin/bash

# ╔═══════════════════════════════════════════════════════════════════════╗
# ║  🔧 TYPE SAFETY VIOLATIONS - AUTOMATED FIX SCRIPT                    ║
# ║  Corrige automatiquement les patterns les plus courants              ║
# ╚═══════════════════════════════════════════════════════════════════════╝

set -e

# Couleurs pour output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Compteurs
FIXED_FILES=0
TOTAL_FIXES=0

echo -e "${BLUE}╔═══════════════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║  🔧 Automatic Type Safety Fixes                                      ║${NC}"
echo -e "${BLUE}╚═══════════════════════════════════════════════════════════════════════╝${NC}"
echo ""

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 1️⃣  FIX: plan.title → plan.name
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
echo -e "${YELLOW}[1/7]${NC} Fixing plan.title → plan.name..."

find apps/web/src -type f \( -name "*.vue" -o -name "*.ts" \) | while read file; do
  if grep -q "plan\.title\|plan\?\.title" "$file" 2>/dev/null; then
    # Property access
    sed -i '' 's/plan\.title/plan.name/g' "$file"
    sed -i '' 's/plan?\.title/plan?.name/g' "$file"
    sed -i '' 's/currentPlan\.title/currentPlan.name/g' "$file"
    sed -i '' 's/currentPlan?\.title/currentPlan?.name/g' "$file"
    sed -i '' 's/existingPlan\.title/existingPlan.name/g' "$file"
    sed -i '' 's/existingPlan?\.title/existingPlan?.name/g' "$file"
    sed -i '' 's/newPlan\.title/newPlan.name/g' "$file"
    
    # Object literals (in TypeScript/JavaScript context)
    sed -i '' "s/title: plan\.name/name: plan.name/g" "$file"
    sed -i '' "s/{ title:/{ name:/g" "$file"
    sed -i '' "s/, title:/, name:/g" "$file"
    
    echo -e "  ${GREEN}✓${NC} Fixed: $file"
    ((FIXED_FILES++))
    ((TOTAL_FIXES++))
  fi
done

echo ""

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 2️⃣  FIX: Phosphor Icons (Remove Ph prefix from imports, keep in template)
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
echo -e "${YELLOW}[2/7]${NC} Fixing Phosphor icon imports..."

find apps/web/src -type f -name "*.vue" | while read file; do
  if grep -q "from '@phosphor-icons/vue'" "$file" 2>/dev/null; then
    # Fix common icons (add more as needed)
    sed -i '' "s/import { Trash }/import { PhTrash }/g" "$file"
    sed -i '' "s/import { DotsSixVertical }/import { PhDotsSixVertical }/g" "$file"
    sed -i '' "s/import { Calendar }/import { PhCalendar }/g" "$file"
    sed -i '' "s/import { MapPin }/import { PhMapPin }/g" "$file"
    sed -i '' "s/import { DotsThree }/import { PhDotsThree }/g" "$file"
    sed -i '' "s/, Trash,/, PhTrash,/g" "$file"
    sed -i '' "s/, Calendar,/, PhCalendar,/g" "$file"
    sed -i '' "s/, MapPin,/, PhMapPin,/g" "$file"
    sed -i '' "s/, DotsThree,/, PhDotsThree,/g" "$file"
    sed -i '' "s/, DotsSixVertical,/, PhDotsSixVertical,/g" "$file"
    
    # Fix template usage
    sed -i '' "s/<Trash/<PhTrash/g" "$file"
    sed -i '' "s/<Calendar/<PhCalendar/g" "$file"
    sed -i '' "s/<MapPin/<PhMapPin/g" "$file"
    sed -i '' "s/<DotsThree/<PhDotsThree/g" "$file"
    sed -i '' "s/<DotsSixVertical/<PhDotsSixVertical/g" "$file"
    
    echo -e "  ${GREEN}✓${NC} Fixed icons: $file"
    ((FIXED_FILES++))
    ((TOTAL_FIXES++))
  fi
done

echo ""

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 3️⃣  FIX: stage.location.name → stage.name
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
echo -e "${YELLOW}[3/7]${NC} Fixing stage.location.name → stage.name..."

find apps/web/src -type f -name "*.vue" | while read file; do
  if grep -q "stage\.location\.name\|stage?\.location\.name" "$file" 2>/dev/null; then
    sed -i '' 's/stage\.location\.name/stage.name/g' "$file"
    sed -i '' 's/stage?\.location\.name/stage?.name/g' "$file"
    
    echo -e "  ${GREEN}✓${NC} Fixed: $file"
    ((FIXED_FILES++))
    ((TOTAL_FIXES++))
  fi
done

echo ""

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 4️⃣  FIX: stage.date → stage.estimatedArrival
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
echo -e "${YELLOW}[4/7]${NC} Fixing stage temporal properties..."

find apps/web/src -type f -name "*.vue" | while read file; do
  if grep -q "stage\.date\|stage\.arrivalDate\|stage\.departureDate" "$file" 2>/dev/null; then
    # stage.date → stage.estimatedArrival (le plus courant)
    sed -i '' 's/formatDate(stage\.date)/formatDate(stage.estimatedArrival)/g' "$file"
    sed -i '' 's/stage\.date/stage.estimatedArrival/g' "$file"
    
    # arrivalDate/departureDate → estimatedArrival/estimatedDeparture
    sed -i '' 's/stage\.arrivalDate/stage.estimatedArrival/g' "$file"
    sed -i '' 's/stage\.departureDate/stage.estimatedDeparture/g' "$file"
    
    echo -e "  ${GREEN}✓${NC} Fixed: $file"
    ((FIXED_FILES++))
    ((TOTAL_FIXES++))
  fi
done

echo ""

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 5️⃣  FIX: CreateStageDTO/UpdateStageDTO title → name
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
echo -e "${YELLOW}[5/7]${NC} Fixing DTO properties..."

find apps/web/src -type f \( -name "*.vue" -o -name "*.ts" \) | while read file; do
  if grep -q "CreateStageDTO\|UpdateStageDTO\|CreatePlanDTO\|UpdatePlanDTO" "$file" 2>/dev/null; then
    # Dans les objets DTO
    sed -i '' "s/title: '\([^']*\)'/name: '\1'/g" "$file"
    sed -i '' 's/title: "\([^"]*\)"/name: "\1"/g' "$file"
    sed -i '' 's/title: newStage\.value\.title/name: newStage.value.name/g' "$file"
    sed -i '' 's/title: stage\.title/name: stage.name/g' "$file"
    
    echo -e "  ${GREEN}✓${NC} Fixed: $file"
    ((FIXED_FILES++))
    ((TOTAL_FIXES++))
  fi
done

echo ""

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 6️⃣  FIX: Remove plan.startDate / plan.endDate references
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
echo -e "${YELLOW}[6/7]${NC} Fixing plan.startDate/endDate (commenting out for manual review)..."

find apps/web/src -type f -name "*.vue" | while read file; do
  if grep -q "plan\.startDate\|plan\.endDate" "$file" 2>/dev/null; then
    # Commenter les lignes qui utilisent startDate/endDate pour revue manuelle
    sed -i '' 's/^\(.*plan\.startDate.*\)$/\/\/ TODO: Fix - plan.startDate no longer exists\n\/\/ \1/g' "$file"
    sed -i '' 's/^\(.*plan\.endDate.*\)$/\/\/ TODO: Fix - plan.endDate no longer exists\n\/\/ \1/g' "$file"
    
    echo -e "  ${YELLOW}⚠${NC}  Commented for review: $file"
    ((FIXED_FILES++))
    ((TOTAL_FIXES++))
  fi
done

echo ""

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 7️⃣  FIX: Test files - Update mock objects
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
echo -e "${YELLOW}[7/7]${NC} Fixing test files..."

find apps/web/src -type f -name "*.test.ts" -o -name "*.spec.ts" | while read file; do
  if [ -f "$file" ]; then
    # Fix mock objects
    sed -i '' "s/title: 'Test/name: 'Test/g" "$file"
    sed -i '' "s/title: '\([^']*\)'/name: '\1'/g" "$file"
    sed -i '' 's/title: Plan/name: Plan/g' "$file"
    
    echo -e "  ${GREEN}✓${NC} Fixed: $file"
    ((FIXED_FILES++))
    ((TOTAL_FIXES++))
  fi
done

echo ""
echo -e "${BLUE}═══════════════════════════════════════════════════════════════════════${NC}"
echo -e "${GREEN}✅ Fixes Applied${NC}"
echo -e "${BLUE}═══════════════════════════════════════════════════════════════════════${NC}"
echo ""
echo -e "  Total files fixed: ${GREEN}${FIXED_FILES}${NC}"
echo -e "  Total fixes applied: ${GREEN}${TOTAL_FIXES}${NC}"
echo ""
echo -e "${YELLOW}⚠️  Next Steps:${NC}"
echo -e "  1. Review changes: ${BLUE}git diff${NC}"
echo -e "  2. Run type-check: ${BLUE}pnpm --filter @my-roadtrip/web type-check${NC}"
echo -e "  3. Fix remaining errors manually"
echo -e "  4. Test the app: ${BLUE}pnpm dev${NC}"
echo ""
echo -e "${BLUE}═══════════════════════════════════════════════════════════════════════${NC}"
