#!/bin/bash

# Blog Post Migration Tool
# Usage: npm run db:migrate-blog
# or: ./scripts/migrate-blog.sh

set -e

echo "🚀 Blog Post Migration Tool"
echo "======================================"
echo ""

# Check if Supabase is running
echo "🔍 Checking Supabase status..."
if ! supabase status &> /dev/null; then
  echo "⚠️  Supabase is not running. Starting local instance..."
  supabase start
  echo "✅ Supabase started successfully"
else
  echo "✅ Supabase is already running"
fi

echo ""
echo "📊 Current database status:"
supabase status | grep -E "API URL|Database URL|Studio URL"

echo ""
echo "📝 Counting blog posts in content directory..."
AR_COUNT=$(ls content/blog/ar/*.mdx 2>/dev/null | wc -l | tr -d ' ')
EN_COUNT=$(ls content/blog/en/*.mdx 2>/dev/null | wc -l | tr -d ' ')
TOTAL_FILES=$((AR_COUNT + EN_COUNT))

echo "   Arabic posts: $AR_COUNT"
echo "   English posts: $EN_COUNT"
echo "   Total files: $TOTAL_FILES"

echo ""
echo "💾 Running migration script..."
npx tsx scripts/migrate-blog-posts.ts

echo ""
echo "✅ Migration complete!"
echo ""
echo "🌐 Access Supabase Studio: http://127.0.0.1:54323"
echo "   View your blog_posts table to verify the data"
echo ""
