#!/bin/bash

# Test TokenFactory138 compilation and check for errors

set -e

cd /home/intlc/projects/proxmox/smom-dbis-138
source scripts/lib/forge-scope.sh

echo "=========================================="
echo "TokenFactory138 Compilation Test"
echo "=========================================="
echo ""

# Check if contract exists
echo "1. Checking if TokenFactory138 contract exists..."
if [ -f "contracts/emoney/TokenFactory138.sol" ]; then
    echo "   ✅ Contract file exists"
else
    echo "   ❌ Contract file not found!"
    exit 1
fi
echo ""

# Check dependencies
echo "2. Checking dependencies..."
MISSING_DEPS=0

check_dep() {
    local file=$1
    if [ -f "$file" ]; then
        echo "   ✅ $file"
    else
        echo "   ❌ $file - MISSING"
        MISSING_DEPS=$((MISSING_DEPS + 1))
    fi
}

check_dep "contracts/emoney/interfaces/ITokenFactory138.sol"
check_dep "contracts/emoney/interfaces/IeMoneyToken.sol"
check_dep "contracts/emoney/interfaces/IPolicyManager.sol"
check_dep "contracts/emoney/eMoneyToken.sol"
check_dep "contracts/emoney/errors/FactoryErrors.sol"
check_dep "contracts/emoney/errors/RegistryErrors.sol"

if [ $MISSING_DEPS -gt 0 ]; then
    echo "   ⚠️  $MISSING_DEPS dependency files missing"
else
    echo "   ✅ All dependencies found"
fi
echo ""

# Try compilation without --via-ir
echo "3. Attempting compilation (standard mode)..."
if forge build --contracts contracts/emoney/TokenFactory138.sol 2>&1 | tee /tmp/tokenfactory-compile-standard.log | grep -q "Compiler run successful"; then
    echo "   ✅ Compilation successful (standard mode)"
    STANDARD_SUCCESS=true
else
    echo "   ⚠️  Compilation failed or had warnings (standard mode)"
    echo "   Checking for errors..."
    grep -i "error\|failed" /tmp/tokenfactory-compile-standard.log | head -20 || echo "   (No clear errors found)"
    STANDARD_SUCCESS=false
fi
echo ""

# Try compilation with --via-ir
echo "4. Attempting compilation (--via-ir mode)..."
if forge build --via-ir --contracts contracts/emoney/TokenFactory138.sol 2>&1 | tee /tmp/tokenfactory-compile-viair.log | grep -q "Compiler run successful"; then
    echo "   ✅ Compilation successful (--via-ir mode)"
    VIAIR_SUCCESS=true
else
    echo "   ⚠️  Compilation failed or had warnings (--via-ir mode)"
    echo "   Checking for errors..."
    grep -i "error\|failed\|stack too deep" /tmp/tokenfactory-compile-viair.log | head -20 || echo "   (No clear errors found)"
    VIAIR_SUCCESS=false
fi
echo ""

# Check for specific errors
echo "5. Checking for specific error patterns..."
if grep -i "stack too deep" /tmp/tokenfactory-compile-standard.log /tmp/tokenfactory-compile-viair.log 2>/dev/null | head -5; then
    echo "   ⚠️  'Stack too deep' error detected - use --via-ir flag"
fi

if grep -i "not found\|missing" /tmp/tokenfactory-compile-standard.log /tmp/tokenfactory-compile-viair.log 2>/dev/null | head -5; then
    echo "   ⚠️  Missing import/dependency detected"
fi

if grep -i "override\|abstract" /tmp/tokenfactory-compile-standard.log /tmp/tokenfactory-compile-viair.log 2>/dev/null | head -5; then
    echo "   ⚠️  Override/abstract issues detected"
fi
echo ""

# Summary
echo "=========================================="
echo "Summary"
echo "=========================================="
echo "Standard compilation: $([ "$STANDARD_SUCCESS" = true ] && echo "✅ Success" || echo "❌ Failed")"
echo "Via-IR compilation:  $([ "$VIAIR_SUCCESS" = true ] && echo "✅ Success" || echo "❌ Failed")"
echo ""

if [ "$VIAIR_SUCCESS" = true ]; then
    echo "✅ TokenFactory138 compiles successfully with --via-ir"
    echo "   Ready for deployment with: --via-ir flag"
elif [ "$STANDARD_SUCCESS" = true ]; then
    echo "✅ TokenFactory138 compiles successfully (standard mode)"
    echo "   Ready for deployment"
else
    echo "❌ TokenFactory138 has compilation errors"
    echo "   Review logs: /tmp/tokenfactory-compile-*.log"
fi
echo ""
