#!/bin/bash

echo "🚀 Delta Lake Go Write Data Demo"
echo "================================"
echo ""

# Clean up any existing temp directories from previous runs
echo "🧹 Cleaning up previous test runs..."
rm -rf /tmp/delta_write_example*

echo "📊 Running Delta Lake write data example..."
echo ""

# Run the example
cd "$(dirname "$0")"
go run write_data.go

echo ""
echo "🔍 Inspecting the results..."

# Find the most recent delta directory
TEMP_DIR=$(ls -td /tmp/delta_write_example* 2>/dev/null | head -1)

if [ -n "$TEMP_DIR" ]; then
    echo "📁 Examining tables in: $TEMP_DIR"
    echo ""
    
    # Show directory structure
    echo "📂 Directory structure:"
    tree "$TEMP_DIR" 2>/dev/null || find "$TEMP_DIR" -type f | sort
    echo ""
    
    # Show file sizes
    echo "📏 File sizes:"
    du -h "$TEMP_DIR"/*/*.parquet
    echo ""
    
    # Count records from Delta logs
    echo "📊 Record counts from Delta logs:"
    echo "Employee table writes:"
    grep -h '"num_added_rows":[0-9]*' "$TEMP_DIR/employee_table/_delta_log/"*.json | \
        sed 's/.*"num_added_rows":\([0-9]*\).*/  Batch: \1 rows/' | nl
    
    echo "Diverse table writes:"
    grep -h '"num_added_rows":[0-9]*' "$TEMP_DIR/diverse_table/_delta_log/"*.json | \
        sed 's/.*"num_added_rows":\([0-9]*\).*/  Batch: \1 rows/' | nl
    
    echo ""
    echo "📈 Version history:"
    echo "Employee table versions: $(ls "$TEMP_DIR/employee_table/_delta_log/"*.json | wc -l | tr -d ' ')"
    echo "Diverse table versions: $(ls "$TEMP_DIR/diverse_table/_delta_log/"*.json | wc -l | tr -d ' ')"
    
    echo ""
    echo "🎯 Summary:"
    EMPLOYEE_TOTAL=$(grep -h '"num_added_rows":[0-9]*' "$TEMP_DIR/employee_table/_delta_log/"*.json | \
        sed 's/.*"num_added_rows":\([0-9]*\).*/\1/' | awk '{sum+=$1} END {print sum}')
    DIVERSE_TOTAL=$(grep -h '"num_added_rows":[0-9]*' "$TEMP_DIR/diverse_table/_delta_log/"*.json | \
        sed 's/.*"num_added_rows":\([0-9]*\).*/\1/' | awk '{sum+=$1} END {print sum}')
    
    echo "  • Employee table: $EMPLOYEE_TOTAL total records"
    echo "  • Diverse table: $DIVERSE_TOTAL total records"
    echo "  • All data successfully written to Delta Lake format"
    echo "  • Parquet files generated with compression"
    echo "  • Transaction logs properly maintained"
    
    echo ""
    echo "✅ Demo completed successfully!"
    echo ""
    echo "💡 You can further inspect the data using:"
    echo "   cat $TEMP_DIR/*//_delta_log//*.json | jq"
    echo "   parquet-tools head $TEMP_DIR/*/*.parquet"
    
else
    echo "❌ No delta tables found!"
    exit 1
fi
