#!/bin/bash
# Test Arabic text overlay functionality

echo "🔄 Testing Arabic text overlay system..."

# Check ffmpeg
if ! command -v ffmpeg &> /dev/null; then
    echo "❌ ffmpeg not found"
    exit 1
fi
echo "✅ ffmpeg found: $(which ffmpeg)"

# Check Arabic fonts
ARABIC_FONTS=$(fc-list | grep -i "noto.*arabic\|cairo\|tajawal" | wc -l)
if [ "$ARABIC_FONTS" -eq 0 ]; then
    echo "❌ No Arabic fonts found"
    exit 1
fi
echo "✅ Arabic fonts found: $ARABIC_FONTS fonts"

# Find an Arabic font
ARABIC_FONT=$(fc-list | grep -i "noto.*naskh.*bold" | head -1 | cut -d: -f1)
if [ -z "$ARABIC_FONT" ]; then
    ARABIC_FONT=$(fc-list | grep -i "noto.*arabic.*bold" | head -1 | cut -d: -f1)
fi
echo "📝 Using font: $ARABIC_FONT"

# Create test video with black background
echo "🎬 Creating test video..."
ffmpeg -f lavfi -i "color=c=black:s=1280x720:d=3" \
       -c:v libx264 -t 3 -y /tmp/test_video.mp4 2>/dev/null

if [ ! -f "/tmp/test_video.mp4" ]; then
    echo "❌ Failed to create test video"
    exit 1
fi
echo "✅ Test video created"

# Create test Arabic text
echo "📝 Creating test Arabic text..."
echo "مرحباً بكم" > /tmp/test_text.txt

# Add Arabic text overlay
echo "🎨 Adding Arabic text overlay..."
ffmpeg -i /tmp/test_video.mp4 \
       -vf "drawtext=textfile=/tmp/test_text.txt:fontsize=60:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:fontfile=$ARABIC_FONT" \
       -c:a copy -y /tmp/test_video_with_text.mp4 2>/dev/null

if [ ! -f "/tmp/test_video_with_text.mp4" ]; then
    echo "❌ Failed to add text overlay"
    exit 1
fi

# Get video duration
DURATION=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 /tmp/test_video_with_text.mp4)
echo "✅ Text overlay successful!"
echo "📹 Output video: /tmp/test_video_with_text.mp4"
echo "⏱️ Duration: ${DURATION}s"

echo ""
echo "🎉 Arabic text overlay system is working!"
echo "You can preview the test video: /tmp/test_video_with_text.mp4"