#!/bin/bash

# Comprehensive compile and test for TokenFactory138

set -e

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

echo "╔══════════════════════════════════════════════════════════════╗"
echo "║     TokenFactory138 Compilation and Error Check              ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""

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

# Step 2: Check all dependencies
echo "Step 2: Checking dependencies..."
DEPS=(
    "contracts/emoney/interfaces/ITokenFactory138.sol"
    "contracts/emoney/interfaces/IeMoneyToken.sol"
    "contracts/emoney/interfaces/IPolicyManager.sol"
    "contracts/emoney/eMoneyToken.sol"
    "contracts/emoney/errors/FactoryErrors.sol"
    "contracts/emoney/errors/RegistryErrors.sol"
)

MISSING=0
for dep in "${DEPS[@]}"; do
    if [ -f "$dep" ]; then
        echo "  ✅ $(basename $dep)"
    else
        echo "  ❌ $(basename $dep) - MISSING"
        MISSING=$((MISSING + 1))
    fi
done

if [ $MISSING -gt 0 ]; then
    echo "⚠️  $MISSING dependencies missing!"
    exit 1
fi
echo "✅ All dependencies found"
echo ""

# Step 3: Try standard compilation
echo "Step 3: Compiling (standard mode)..."
if forge build --contracts contracts/emoney/TokenFactory138.sol 2>&1 | tee /tmp/tf138-std.log | tail -5 | grep -q "Compiler run successful"; then
    echo "✅ Standard compilation: SUCCESS"
    STD_OK=true
else
    echo "❌ Standard compilation: FAILED"
    STD_OK=false
    echo "Errors:"
    grep -i "error" /tmp/tf138-std.log | head -10 || echo "  (check log file)"
fi
echo ""

# Step 4: Try via-ir compilation
echo "Step 4: Compiling (--via-ir mode)..."
if forge build --via-ir --contracts contracts/emoney/TokenFactory138.sol 2>&1 | tee /tmp/tf138-viair.log | tail -5 | grep -q "Compiler run successful"; then
    echo "✅ Via-IR compilation: SUCCESS"
    VIAIR_OK=true
else
    echo "❌ Via-IR compilation: FAILED"
    VIAIR_OK=false
    echo "Errors:"
    grep -i "error\|stack too deep" /tmp/tf138-viair.log | head -10 || echo "  (check log file)"
fi
echo ""

# Step 5: Check for specific issues
echo "Step 5: Checking for specific issues..."

if grep -qi "stack too deep" /tmp/tf138-std.log 2>/dev/null; then
    echo "⚠️  'Stack too deep' error in standard mode - use --via-ir"
fi

if grep -qi "not found\|missing" /tmp/tf138-std.log /tmp/tf138-viair.log 2>/dev/null; then
    echo "⚠️  Missing imports/dependencies detected"
    grep -i "not found\|missing" /tmp/tf138-std.log /tmp/tf138-viair.log 2>/dev/null | head -5
fi

if grep -qi "override" /tmp/tf138-std.log /tmp/tf138-viair.log 2>/dev/null; then
    echo "⚠️  Override issues detected"
    grep -i "override" /tmp/tf138-std.log /tmp/tf138-viair.log 2>/dev/null | head -5
fi
echo ""

# Step 6: Summary
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║     Summary                                                  ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""

if [ "$VIAIR_OK" = true ]; then
    echo "✅ TokenFactory138 compiles successfully with --via-ir"
    echo ""
    echo "Ready for deployment with:"
    echo "  forge script script/emoney/DeployChain138.s.sol:DeployChain138 \\"
    echo "    --rpc-url \$RPC_URL \\"
    echo "    --broadcast \\"
    echo "    --legacy \\"
    echo "    --gas-price 20000000000 \\"
    echo "    --via-ir \\"
    echo "    -vv"
elif [ "$STD_OK" = true ]; then
    echo "✅ TokenFactory138 compiles successfully (standard mode)"
    echo ""
    echo "Ready for deployment (may need --via-ir if stack too deep during deployment)"
else
    echo "❌ TokenFactory138 has compilation errors"
    echo ""
    echo "Review logs:"
    echo "  /tmp/tf138-std.log"
    echo "  /tmp/tf138-viair.log"
    echo ""
    echo "Common fixes:"
    echo "  1. Use --via-ir flag for 'stack too deep' errors"
    echo "  2. Check all imports are correct"
    echo "  3. Ensure all dependencies are compiled"
fi
echo ""
