#!/bin/bash

# Simple Content Normalization Script
# Normalizes a few key categories that need fixes and adds slug fields

set -e

CONTENT_ROOT="content"
BACKUP_DIR="content-backup-$(date +%Y%m%d_%H%M%S)"

echo "🔄 Starting simple content normalization"
echo "📁 Content root: $CONTENT_ROOT"
echo "💾 Backup will be created at: $BACKUP_DIR"

# Create backup
echo "📦 Creating backup..."
cp -r "$CONTENT_ROOT" "$BACKUP_DIR"
echo "✅ Backup created successfully"

# Fix specific categories that need normalization
echo "🔄 Normalizing specific categories..."

# Fix "Rust Programming" -> "rust-programming"
find "$CONTENT_ROOT" -name "*.json" -exec sed -i '' 's/"Rust Programming"/"rust-programming"/g' {} \;
find "$CONTENT_ROOT" -name "*.md" -exec sed -i '' 's/category: "Rust Programming"/category: "rust-programming"/g' {} \;

# Fix "Async Programming" -> "async-programming"
find "$CONTENT_ROOT" -name "*.json" -exec sed -i '' 's/"Async Programming"/"async-programming"/g' {} \;
find "$CONTENT_ROOT" -name "*.md" -exec sed -i '' 's/category: "Async Programming"/category: "async-programming"/g' {} \;

# Fix "Error Handling" -> "error-handling"
find "$CONTENT_ROOT" -name "*.json" -exec sed -i '' 's/"Error Handling"/"error-handling"/g' {} \;
find "$CONTENT_ROOT" -name "*.md" -exec sed -i '' 's/"Error Handling"/"error-handling"/g' {} \;

# Fix "Best Practices" -> "best-practices"
find "$CONTENT_ROOT" -name "*.json" -exec sed -i '' 's/"Best Practices"/"best-practices"/g' {} \;
find "$CONTENT_ROOT" -name "*.md" -exec sed -i '' 's/"Best Practices"/"best-practices"/g' {} \;

# Fix "Cloud Native" -> "cloud-native"
find "$CONTENT_ROOT" -name "*.json" -exec sed -i '' 's/"Cloud Native"/"cloud-native"/g' {} \;
find "$CONTENT_ROOT" -name "*.md" -exec sed -i '' 's/"Cloud Native"/"cloud-native"/g' {} \;

# Fix Spanish categories
find "$CONTENT_ROOT" -name "*.json" -exec sed -i '' 's/"Programación Rust"/"programacion-rust"/g' {} \;
find "$CONTENT_ROOT" -name "*.md" -exec sed -i '' 's/category: "Programación Rust"/category: "programacion-rust"/g' {} \;

find "$CONTENT_ROOT" -name "*.json" -exec sed -i '' 's/"Programación Asíncrona"/"programacion-asincrona"/g' {} \;
find "$CONTENT_ROOT" -name "*.md" -exec sed -i '' 's/category: "Programación Asíncrona"/category: "programacion-asincrona"/g' {} \;

find "$CONTENT_ROOT" -name "*.json" -exec sed -i '' 's/"Manejo de Errores"/"manejo-de-errores"/g' {} \;
find "$CONTENT_ROOT" -name "*.md" -exec sed -i '' 's/"Manejo de Errores"/"manejo-de-errores"/g' {} \;

find "$CONTENT_ROOT" -name "*.json" -exec sed -i '' 's/"Mejores Prácticas"/"mejores-practicas"/g' {} \;
find "$CONTENT_ROOT" -name "*.md" -exec sed -i '' 's/"Mejores Prácticas"/"mejores-practicas"/g' {} \;

# Function to add slug to markdown files that don't have one
add_slug_to_markdown() {
    local file="$1"

    # Check if slug already exists
    if grep -q "^slug:" "$file"; then
        return 0
    fi

    # Extract title
    local title=$(grep "^title:" "$file" | head -1 | sed 's/title: *"*\([^"]*\)"*.*/\1/')

    if [[ -n "$title" ]]; then
        # Create slug from title
        local slug=$(echo "$title" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/--*/-/g' | sed 's/^-\|-$//g')

        # Add slug after title
        sed -i '' "/^title:/a\\
slug: \"$slug\"" "$file"

        echo "  🔗 Added slug '$slug' to $file"
    fi
}

# Add slugs to markdown files
echo "🔄 Adding slugs to markdown files..."
find "$CONTENT_ROOT" -name "*.md" | while read -r file; do
    add_slug_to_markdown "$file"
done

echo ""
echo "✅ Content normalization completed successfully!"
echo "📁 Backup location: $BACKUP_DIR"
echo ""
echo "📋 Summary of changes:"
echo "   • Fixed 'Rust Programming' -> 'rust-programming'"
echo "   • Fixed 'Async Programming' -> 'async-programming'"
echo "   • Fixed 'Error Handling' -> 'error-handling'"
echo "   • Fixed 'Best Practices' -> 'best-practices'"
echo "   • Fixed 'Cloud Native' -> 'cloud-native'"
echo "   • Fixed Spanish equivalents"
echo "   • Added slug fields where missing"
echo ""
echo "🔧 Next steps:"
echo "   1. Test the application"
echo "   2. If satisfied, remove the backup: rm -rf $BACKUP_DIR"
