#!/bin/bash

# Fix Type Definitions - Missing Properties
# Date: 2025-10-29

set -e

echo "🔍 Analyzing type issues and fixing..."

TYPES_DIR="packages/types/src"

# ============================================
# 1. Add missing properties to Plan type
# ============================================
echo "1️⃣ Adding missing properties to Plan type..."

cat > /tmp/plan_additions.txt << 'EOF'

  // UI-only properties (not stored in backend)
  coverImage?: string
  startDate?: Date
  endDate?: Date
  budget?: number
  tags?: string[]
EOF

# Check if properties already exist
if ! grep -q "coverImage" "$TYPES_DIR/planner.types.ts"; then
  # Find line with "updatedAt: Date" and add after it
  sed -i '' '/updatedAt: Date$/a\
\
  // UI-only properties (not stored in backend)\
  coverImage?: string\
  startDate?: Date\
  endDate?: Date\
  budget?: number\
  tags?: string[]
' "$TYPES_DIR/planner.types.ts"
  echo "   ✅ Added coverImage, startDate, endDate to Plan"
else
  echo "   ⏭️  Properties already exist"
fi

# ============================================
# 2. Add name property to MapLayer
# ============================================
echo "2️⃣ Adding name property to MapLayer..."

if ! grep -q "name:" "$TYPES_DIR/map.types.ts"; then
  sed -i '' '/export interface MapLayer {/a\
  name: string
' "$TYPES_DIR/map.types.ts"
  echo "   ✅ Added name to MapLayer"
else
  echo "   ⏭️  name already exists in MapLayer"
fi

# ============================================
# 3. Add timestamp to Achievement
# ============================================
echo "3️⃣ Fixing Achievement type..."

if grep -q "createdAt: Date" "$TYPES_DIR/gamification.types.ts"; then
  # Achievement has createdAt, add timestamp alias
  sed -i '' '/export interface Achievement {/,/^}/ s/createdAt: Date/createdAt: Date\
  timestamp: Date // Alias for createdAt/' "$TYPES_DIR/gamification.types.ts"
  echo "   ✅ Added timestamp alias to Achievement"
fi

# ============================================
# 4. Export Share type (alias for ShareLink)
# ============================================
echo "4️⃣ Adding Share type alias..."

if ! grep -q "export type Share" "$TYPES_DIR/sharing.types.ts"; then
  cat >> "$TYPES_DIR/sharing.types.ts" << 'EOF'

// Type alias for compatibility
export type Share = ShareLink
EOF
  echo "   ✅ Added Share type alias"
else
  echo "   ⏭️  Share type already exists"
fi

# ============================================
# 5. Fix CreatePlanDTO to use 'name' instead of 'title'
# ============================================
echo "5️⃣ Checking CreatePlanDTO..."

if grep -q "title: string" "$TYPES_DIR/planner.types.ts"; then
  sed -i '' 's/title: string/name: string/g' "$TYPES_DIR/planner.types.ts"
  echo "   ✅ Changed title to name in CreatePlanDTO"
else
  echo "   ⏭️  CreatePlanDTO already uses 'name'"
fi

# ============================================
# 6. Add notes to CreateStageDTO if missing
# ============================================
echo "6️⃣ Checking CreateStageDTO..."

# Find CreateStageDTO and check if notes exists
if grep -A 10 "export interface CreateStageDTO" "$TYPES_DIR/planner.types.ts" | grep -q "notes"; then
  echo "   ⏭️  notes already in CreateStageDTO"
else
  # Add notes after location
  sed -i '' '/export interface CreateStageDTO {/,/^}/ s/location: Location/location: Location\
  notes?: string/' "$TYPES_DIR/planner.types.ts"
  echo "   ✅ Added notes to CreateStageDTO"
fi

# ============================================
# 7. Re-export types in index.ts
# ============================================
echo "7️⃣ Updating index.ts exports..."

cat > "$TYPES_DIR/index.ts" << 'EOF'
// Core types
export * from './planner.types'
export * from './map.types'
export * from './collab.types'
export * from './sharing.types'
export * from './gamification.types'
export * from './search.types'
export * from './admin.types'

// Re-export commonly used types with aliases for compatibility
export type { ShareLink as Share } from './sharing.types'
EOF

echo "   ✅ Updated index.ts"

echo ""
echo "✅ Type fixes completed!"
echo ""
echo "Run: pnpm --filter @my-roadtrip/types build"
echo "Then: pnpm --filter @my-roadtrip/web type-check"
