#!/bin/bash
# Script de migration automatique: UserID int64 → uuid.UUID dans tous les modèles
# Date: 2024-11-27
# Usage: ./scripts/migrate-models-to-uuid.sh

set -e

MODELS_DIR="veza-backend-api/internal/models"
BACKUP_DIR="veza-backend-api/internal/models/.backup-pre-uuid-migration"

echo "🔧 Migration UUID - Début"
echo "========================="

# Créer un backup des modèles
echo "📦 Création backup dans $BACKUP_DIR..."
mkdir -p "$BACKUP_DIR"
cp -r "$MODELS_DIR"/*.go "$BACKUP_DIR/" 2>/dev/null || true

# Liste des fichiers à modifier (déjà traités: user.go, role.go, playlist.go, track.go, refresh_token.go)
FILES_TO_MIGRATE=(
    "message.go"
    "room.go"
    "track_comment.go"
    "track_like.go"
    "track_share.go"
    "track_history.go"
    "playlist_collaborator.go"
    "playlist_follow.go"
    "playlist_share_link.go"
    "playlist_version.go"
    "playback_analytics.go"
    "bitrate_adaptation.go"
    "user_settings.go"
    "hardware.go"
    "contest.go"
)

echo "📝 Fichiers à migrer: ${#FILES_TO_MIGRATE[@]}"

for file in "${FILES_TO_MIGRATE[@]}"; do
    filepath="$MODELS_DIR/$file"
    
    if [ ! -f "$filepath" ]; then
        echo "⏭️  $file non trouvé, skip"
        continue
    fi
    
    echo "🔄 Migration de $file..."
    
    # 1. Ajouter import uuid si pas présent
    if ! grep -q '"github.com/google/uuid"' "$filepath"; then
        # Ajouter après l'import de time ou gorm
        sed -i '/import (/a\	"github.com/google/uuid"' "$filepath" || \
        sed -i 's|"gorm.io/gorm"|"github.com/google/uuid"\n\t"gorm.io/gorm"|' "$filepath"
    fi
    
    # 2. Remplacer UserID int64 par uuid.UUID
    sed -i 's/UserID\s\+int64\s\+`gorm/UserID      uuid.UUID `gorm:"type:uuid;/g' "$filepath"
    
    # 3. Remplacer SenderID int64 (pour messages)
    sed -i 's/SenderID\s\+int64\s\+`gorm/SenderID    uuid.UUID `gorm:"type:uuid;/g' "$filepath"
    
    # 4. Remplacer OwnerID int64 (pour rooms, hardware)
    sed -i 's/OwnerID\s\+int64\s\+`gorm/OwnerID     uuid.UUID `gorm:"type:uuid;/g' "$filepath"
    
    # 5. Remplacer CreatedBy/UpdatedBy/DeletedBy int64
    sed -i 's/CreatedBy\s\+\*\?int64\s\+`gorm/CreatedBy   *uuid.UUID `gorm:"type:uuid;/g' "$filepath"
    sed -i 's/UpdatedBy\s\+\*\?int64\s\+`gorm/UpdatedBy   *uuid.UUID `gorm:"type:uuid;/g' "$filepath"
    sed -i 's/DeletedBy\s\+\*\?int64\s\+`gorm/DeletedBy   *uuid.UUID `gorm:"type:uuid;/g' "$filepath"
    
    # 6. Remplacer AssignedBy int64 (déjà fait dans role.go mais pour cohérence)
    sed -i 's/AssignedBy\s\+\*\?int64\s\+`gorm/AssignedBy  *uuid.UUID `gorm:"type:uuid;/g' "$filepath"
    
    echo "✅ $file migré"
done

echo ""
echo "========================="
echo "✅ Migration UUID terminée"
echo "📦 Backup disponible dans: $BACKUP_DIR"
echo ""
echo "⚠️  PROCHAINES ÉTAPES:"
echo "1. Vérifier les fichiers migrés: git diff veza-backend-api/internal/models/"
echo "2. Compiler: cd veza-backend-api && go build ./..."
echo "3. Exécuter migration SQL: psql < migrations/047_migrate_users_id_to_uuid.sql"
echo "4. Tester: go test ./..."

