#!/bin/bash

# Test script to verify the write_data example
echo "🧪 Testing Delta Lake Write Data Example"
echo "========================================"

# Run the example
echo "📊 Running write_data example..."
cd "$(dirname "$0")"
go run write_data.go > test_output.txt 2>&1

# Check if the example ran successfully
if [ $? -eq 0 ]; then
    echo "✅ Example executed successfully!"
else
    echo "❌ Example failed to execute!"
    cat test_output.txt
    exit 1
fi

# Extract the temporary directory from output
TEMP_DIR=$(grep "Tables stored in:" test_output.txt | sed 's/.*Tables stored in: //')
echo "📁 Tables stored in: $TEMP_DIR"

# Verify table structure
echo ""
echo "🔍 Verifying table structure..."

# Check employee table
if [ -d "$TEMP_DIR/employee_table" ]; then
    echo "✅ Employee table directory exists"
    
    # Check delta log files
    LOG_COUNT=$(ls "$TEMP_DIR/employee_table/_delta_log/"*.json 2>/dev/null | wc -l)
    echo "📄 Delta log files: $LOG_COUNT (expected: 3)"
    
    # Check data files
    DATA_COUNT=$(ls "$TEMP_DIR/employee_table/"*.parquet 2>/dev/null | wc -l)
    echo "🗃️  Data files: $DATA_COUNT (expected: 2)"
    
    # Check for metadata in log files
    if grep -q "num_added_rows" "$TEMP_DIR/employee_table/_delta_log/"*.json; then
        echo "✅ Metadata contains row count information"
    else
        echo "❌ Missing row count metadata"
    fi
    
    # Verify records count from metadata
    TOTAL_ROWS=$(grep -o '"num_added_rows":[0-9]*' "$TEMP_DIR/employee_table/_delta_log/"*.json | awk -F: '{sum+=$2} END {print sum}')
    echo "📊 Total rows written: $TOTAL_ROWS (expected: 6)"
    
else
    echo "❌ Employee table directory not found!"
    exit 1
fi

# Check diverse table
if [ -d "$TEMP_DIR/diverse_table" ]; then
    echo "✅ Diverse table directory exists"
    
    # Check delta log files
    LOG_COUNT_D=$(ls "$TEMP_DIR/diverse_table/_delta_log/"*.json 2>/dev/null | wc -l)
    echo "📄 Delta log files: $LOG_COUNT_D (expected: 2)"
    
    # Check data files
    DATA_COUNT_D=$(ls "$TEMP_DIR/diverse_table/"*.parquet 2>/dev/null | wc -l)
    echo "🗃️  Data files: $DATA_COUNT_D (expected: 1)"
    
else
    echo "❌ Diverse table directory not found!"
    exit 1
fi

# Verify output contains expected success messages
echo ""
echo "🔍 Verifying output messages..."

if grep -q "✅ First batch written successfully!" test_output.txt; then
    echo "✅ First batch write confirmed"
else
    echo "❌ First batch write not confirmed"
fi

if grep -q "✅ Second batch written successfully!" test_output.txt; then
    echo "✅ Second batch write confirmed"
else
    echo "❌ Second batch write not confirmed"
fi

if grep -q "✅ Diverse data types written successfully!" test_output.txt; then
    echo "✅ Diverse data write confirmed"
else
    echo "❌ Diverse data write not confirmed"
fi

# Check version progression
VERSION_COUNT=$(grep -c "Table version" test_output.txt)
echo "📈 Version updates tracked: $VERSION_COUNT (expected: 4)"

# Display file sizes for verification
echo ""
echo "📏 File size verification:"
du -h "$TEMP_DIR/employee_table/"*.parquet 2>/dev/null | while read size file; do
    filename=$(basename "$file")
    echo "   Employee data file: $filename ($size)"
done

du -h "$TEMP_DIR/diverse_table/"*.parquet 2>/dev/null | while read size file; do
    filename=$(basename "$file")
    echo "   Diverse data file: $filename ($size)"
done

# Final summary
echo ""
echo "🎯 Test Summary:"
echo "   ✅ Tables created successfully"
echo "   ✅ Data written in multiple batches"
echo "   ✅ Version tracking working"
echo "   ✅ Multiple data types supported"
echo "   ✅ Parquet files generated correctly"
echo "   ✅ Delta log files present"

echo ""
echo "🧹 Cleaning up test files..."
rm -f test_output.txt
# Uncomment the next line to clean up the delta tables as well
# rm -rf "$TEMP_DIR"

echo "✅ Write Data Example Test Completed Successfully!"
