#!/usr/bin/env bash
set -euo pipefail

# Cross-compilation build script for NextGen Go program only
# This script builds Go binary for x86_64 Linux using Docker
# Note: Use build_rust_lib.sh separately to build Rust library

ROOT=$(cd $(dirname ${BASH_SOURCE[0]})/../../../ && pwd)
cd ${ROOT}

echo " Cross-compiling NextGen Go program for x86_64 Linux..."
echo "Note: Building Go binary with CGO (DuckDB + delta-rs-golang dependencies)"
echo "Note: vendor_cross_compile directory will be created and cleaned up automatically"

# Create build/bin/x86 directory if it doesn't exist
mkdir -p ${ROOT}/build/bin/x86

# Check if Rust library exists
if [[ ! -f "${ROOT}/build/so/libdeltalake_golang.so" ]]; then
    echo "⚠️  Warning: Rust library not found at ${ROOT}/build/so/libdeltalake_golang.so"
    echo "💡 Please run: cd ${ROOT}/build/package/nextgen && ./build_rust_lib.sh"
    echo "❌ Aborting Go cross-compilation"
    exit 1
fi

echo "✅ Found Rust library at ${ROOT}/build/so/libdeltalake_golang.so"

# Create vendor directory for cross-compilation
echo "📦 Creating vendor directory for cross-compilation..."
cd ${ROOT}

# Remove existing vendor_cross_compile if it exists
if [[ -d "vendor_cross_compile" ]]; then
    echo "🗑️  Removing existing vendor_cross_compile directory..."
    rm -rf vendor_cross_compile
fi

# Create vendor directory
go mod vendor

# Rename to vendor_cross_compile to avoid using vendor mode during development
if [[ -d "vendor" ]]; then
    mv vendor vendor_cross_compile
    echo "✅ Vendor directory created and renamed to vendor_cross_compile"
else
    echo "❌ Failed to create vendor directory"
    exit 1
fi


# Check available disk space
echo "💽 Checking available disk space..."
df -h /var/lib/docker 2>/dev/null || df -h /

# Build using Docker cross-compilation
cd ${ROOT}/build/package/nextgen

echo "🐳 Building Go binary in Docker container with cross-compilation toolchain..."
echo "Note: vendor_cross_compile directory will be used directly by Dockerfile"

docker build --memory=4g --memory-swap=4g -f Dockerfile.cross -t nextgen-go-cross-builder ${ROOT}

echo "📦 Extracting cross-compiled Go binary from Docker container..."
# Create a temporary container and copy the binary
CONTAINER_ID=$(docker create nextgen-go-cross-builder)

# Copy Go binary to x86 directory
docker cp ${CONTAINER_ID}:/tmp/nextgen-metering-server ${ROOT}/build/bin/x86/

# Cleanup container
docker rm ${CONTAINER_ID}

echo "✅ Go cross-compilation completed successfully!"

# Clean up vendor_cross_compile directory to avoid interference with development
echo "🧹 Cleaning up vendor_cross_compile directory..."
cd ${ROOT}
if [[ -d "vendor_cross_compile" ]]; then
    rm -rf vendor_cross_compile
    echo "✅ vendor_cross_compile directory removed"
fi

# Verify the built file
if [[ -f "${ROOT}/build/bin/x86/nextgen-metering-server" ]]; then
    echo "📋 Go binary info:"
    ls -la "${ROOT}/build/bin/x86/nextgen-metering-server"
    file "${ROOT}/build/bin/x86/nextgen-metering-server" || echo "file command not available"
else
    echo "❌ Failed to build Go binary"
    exit 1
fi

echo ""
echo "🎯 Cross-compiled Go binary ready:"
echo "   - Go binary: ${ROOT}/build/bin/x86/nextgen-metering-server"
echo "   - Rust library: ${ROOT}/build/so/libdeltalake_golang.so (pre-built)"
echo ""
echo "💡 You can now run 'cd ${ROOT}/build/package/nextgen && ./aws_dev.sh' to deploy"