#!/bin/bash
set -e

docker_pull_with_retries() {
    local image="$1"
    local err_file
    local attempt
    local max_attempts=3
    local delay=5

    err_file=$(mktemp)

    for attempt in $(seq 1 "$max_attempts"); do
        if docker pull --platform linux/amd64 -q "$image" > /dev/null 2>"$err_file"; then
            rm -f "$err_file"
            return 0
        fi

        if [ "$attempt" -lt "$max_attempts" ]; then
            sleep "$delay"
            delay=$((delay * 2))
        fi
    done

    cat "$err_file" >&2
    rm -f "$err_file"
    return 1
}

# Check for new major.minor versions
echo "🔍 Checking for new MariaDB major.minor versions..."

LATEST_MARIADB="12.3"

if command -v curl >/dev/null 2>&1; then
    found_new=false

    mariadb_versions=$(curl -s "https://hub.docker.com/v2/repositories/library/mariadb/tags/?page_size=100" 2>/dev/null | grep -o '"name":"[0-9]*\.[0-9]*"' | grep -o '[0-9]*\.[0-9]*' | sort -V)

    newer_mariadb=$(echo "$mariadb_versions" | awk -v latest="$LATEST_MARIADB" '
    function version_compare(v1, v2) {
        split(v1, a, ".")
        split(v2, b, ".")
        if (a[1] != b[1]) return a[1] - b[1]
        return a[2] - b[2]
    }
    version_compare($0, latest) > 0 { print $0 }')

    if [ -n "$newer_mariadb" ]; then
        echo "🆕 NEW MariaDB versions detected:"
        echo "$newer_mariadb" | sed 's/^/  - mariadb:/'
        found_new=true
    fi

    if [ "$found_new" = false ]; then
        echo "✅ No new versions found after MariaDB $LATEST_MARIADB"
    else
        echo "❗ Please update the versions array and test new versions!"
        echo "❗ Also update:"
        echo "   - LATEST_MARIADB variable in this script"
        echo "   - Version numbers in documentation (manual/english/Securing_and_compacting_a_table/Backup_and_restore.md)"
        exit 1
    fi

    echo "✅ Version check completed"
else
    echo "⚠️ curl not available - skipping major version check"
fi
echo ""

# MariaDB versions
versions=("mariadb:10.5" "mariadb:10.6" "mariadb:10.7" "mariadb:10.8" "mariadb:10.9" "mariadb:10.10" "mariadb:10.11" "mariadb:11.0" "mariadb:11.1" "mariadb:11.2" "mariadb:11.3-rc" "mariadb:11.4" "mariadb:11.5" "mariadb:11.6" "mariadb:11.7" "mariadb:11.8" "mariadb:12.0" "mariadb:12.1" "mariadb:12.2" "mariadb:12.3" "mariadb:latest")

# Going through all the versions
for version in "${versions[@]}"; do
    db_type="mariadb"
    dump_command="mariadb-dump"

    echo "Testing version: $version"

    # Start the container
    docker_pull_with_retries "$version"
    docker run --rm -d --network=test_network --platform linux/amd64 --name db-test -e MYSQL_ROOT_PASSWORD=my-secret-pw $version bash -c "tail -f /dev/null" > /dev/null
    sleep 5

    # Executing dump
    docker exec db-test $dump_command --skip-ssl-verify-server-cert --skip-no-autocommit -hmanticore -P9306 manticore t > dump.sql 2> >(grep -E -v "Warning: column statistics|Warning: version string returned by server is incorrect." >&2)
    docker exec manticore mysql -h0 -P9306 -e "DROP TABLE t;"

    # Restore dump
    docker exec -i db-test $db_type --skip-ssl-verify-server-cert -hmanticore -P9306 manticore < dump.sql
    restore_status=$?
    
    # Clear query log before test query
    docker exec manticore bash -c "> /var/log/manticore/query.log" 2>/dev/null || true
    
    # Execute test query (only if restore succeeded)
    if [ $restore_status -eq 0 ]; then
        docker exec db-test $db_type --skip-ssl-verify-server-cert -hmanticore -t -P9306 -e "select * from t order by id asc limit 20;" manticore
    else
        echo "⚠️ Warning: Restore failed for $version, skipping query log check"
    fi
    
    # Output query log content (only if restore succeeded) - CLT will validate against expected output
    if [ $restore_status -eq 0 ]; then
        echo "   Query log content:"
        docker exec manticore cat /var/log/manticore/query.log 2>/dev/null || echo "   (empty)"
    fi

    # Checking for errors
    if [ -s dump.sql ]; then
        echo "Dump $version completed successfully"
    else
        echo "Error: dump.sql is empty for $version"
    fi

    # Test replicated table (cluster:table syntax) - Issue #3047
    echo "Testing cluster:table with $version..."
    
    # Create cluster and replicated table
    docker exec manticore mysql -h0 -P9306 -e "DELETE CLUSTER test_c;" 2>/dev/null || true
    docker exec manticore mysql -h0 -P9306 -e "CREATE CLUSTER test_c;"
    docker exec manticore mysql -h0 -P9306 -e "CREATE TABLE test_repl (id BIGINT, data TEXT);"
    docker exec manticore mysql -h0 -P9306 -e "ALTER CLUSTER test_c ADD test_repl;"
    docker exec manticore mysql -h0 -P9306 -e "INSERT INTO test_c:test_repl (id, data) VALUES (1, 'cluster_data');"
    
    # Test 1: mariadb-dump WITHOUT --skip-lock-tables should fail
    echo "  Testing WITHOUT --skip-lock-tables (should fail)..."
    if docker exec db-test $dump_command --skip-ssl-verify-server-cert -hmanticore -P9306 -ucluster -t manticore test_c:test_repl > cluster_dump_fail.sql 2>&1; then
        echo "  ⚠️  WARNING: mariadb-dump succeeded without --skip-lock-tables (bug may be fixed)"
    else
        echo "  ✅ Expected: mariadb-dump failed without --skip-lock-tables"
    fi
    
    # Test 2: mariadb-dump WITH --skip-lock-tables should succeed
    echo "  Testing WITH --skip-lock-tables (should succeed)..."
    if docker exec db-test $dump_command --skip-ssl-verify-server-cert -hmanticore -P9306 -ucluster -t --skip-lock-tables --compact manticore test_c:test_repl > cluster_dump_ok.sql 2> >(grep -E -v "Warning: column statistics|Warning: version string returned by server is incorrect|Couldn't read status information" >&2); then
        if grep -q "cluster_data" cluster_dump_ok.sql; then
            echo "  ✅ Workaround successful: dump contains data with --skip-lock-tables"
        else
            echo "  ❌ ERROR: dump succeeded but data missing"
        fi
    else
        echo "  ❌ ERROR: mariadb-dump failed even with --skip-lock-tables"
    fi
    
    # Cleanup cluster test
    docker exec manticore mysql -h0 -P9306 -e "ALTER CLUSTER test_c DROP test_repl;" 2>/dev/null || true
    docker exec manticore mysql -h0 -P9306 -e "DELETE CLUSTER test_c;" 2>/dev/null || true
    docker exec manticore mysql -h0 -P9306 -e "DROP TABLE IF EXISTS test_repl;" 2>/dev/null || true
    rm -f cluster_dump_fail.sql cluster_dump_ok.sql

    # Stopping and deleting a container
    docker stop db-test > /dev/null
    rm -f dump.sql 2>/dev/null || true
done

echo "All MariaDB versions tested successfully!"

# Check documentation versions
echo ""
echo "Checking documentation versions..."

# Check documentation from mounted /manual volume, with local-checkout fallback
DOC_FILE="/manual/english/Securing_and_compacting_a_table/Backup_and_restore.md"
if [ ! -f "$DOC_FILE" ] && [ -f "./manual/english/Securing_and_compacting_a_table/Backup_and_restore.md" ]; then
    DOC_FILE="./manual/english/Securing_and_compacting_a_table/Backup_and_restore.md"
fi

if [ -f "$DOC_FILE" ]; then
    echo "Checking documentation file..."

    # Extract versions from documentation
    DOC_MARIADB=$(grep -o "MariaDB up to [0-9]\+\.[0-9]\+" "$DOC_FILE" | grep -o "[0-9]\+\.[0-9]\+" | head -1)

    echo "Script version: MariaDB $LATEST_MARIADB"
    echo "Documentation version: MariaDB ${DOC_MARIADB:-not found}"

    # Check if they match
    if [ "$DOC_MARIADB" = "$LATEST_MARIADB" ]; then
        echo "✅ Documentation MariaDB version matches script version"
    else
        echo "❌ Documentation MariaDB version doesn't match script version!"
        echo ""
        echo "Script has: MariaDB $LATEST_MARIADB"
        echo "Documentation has: MariaDB ${DOC_MARIADB:-not found}"
        echo ""
        echo "Please update documentation file:"
        echo "manual/english/Securing_and_compacting_a_table/Backup_and_restore.md"
        exit 1
    fi
else
    echo "⚠️ Documentation file not found at $DOC_FILE"
    echo "Make sure manual directory is mounted with -v"
    exit 1
fi

exit 0
