#!/bin/bash

# Run all unit tests for all languages

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"

echo "========================================="
echo "Running Unit Tests for All Languages"
echo "========================================="
echo ""

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

total_passed=0
total_failed=0
total_languages=0

# List of all languages
LANGUAGES="bash brainfuck c cpp crystal csharp d dart elixir erlang fortran go haskell java javascript julia kotlin objective-c scala typescript lua nim odin perl php python r ruby rust swift zig assembly wolfram vimscript"

# Test each language
for lang in $LANGUAGES; do
    echo "Testing $lang..."
    if [ -f "$lang/cmd/test.sh" ]; then
        cd "$lang"
        if cmd/test.sh 2>&1; then
            echo -e "${GREEN}✓${NC} $lang tests passed"
            ((total_passed++))
        else
            echo -e "${RED}✗${NC} $lang tests failed"
            ((total_failed++))
        fi
        cd "$SCRIPT_DIR"
    else
        echo "⊘ $lang tests skipped (no cmd/test.sh)"
    fi
    ((total_languages++))
    echo ""
done

echo "========================================="
echo "Test Summary"
echo "========================================="
echo "Total languages: $total_languages"
echo -e "Passed: ${GREEN}$total_passed${NC}"
echo -e "Failed: ${RED}$total_failed${NC}"

if [ $total_failed -eq 0 ]; then
    echo -e "${GREEN}✓ All tests passed!${NC}"
    exit 0
else
    echo -e "${RED}✗ Some tests failed${NC}"
    exit 1
fi
fi
((total_languages++))
echo ""

# Go
echo "Testing Go..."
cd /Users/einand/Code/test/go
if go test -v 2>&1 | grep -E "PASS"; then
    echo "✓ Go tests passed"
    ((total_passed++))
else
    echo "✗ Go tests failed"
    ((total_failed++))
fi
((total_languages++))
echo ""

# Java
echo "Testing Java..."
cd /Users/einand/Code/test/java
if [ -f PiTest.class ] || javac -cp .:junit-4.13.2.jar:hamcrest-core-1.3.jar PiTest.java 2>/dev/null; then
    if java -cp .:junit-4.13.2.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore PiTest 2>&1 | grep -E "OK"; then
        echo "✓ Java tests passed"
        ((total_passed++))
    else
        echo "✗ Java tests failed"
        ((total_failed++))
    fi
else
    echo "⊘ Java tests skipped (JUnit not available)"
fi
((total_languages++))
echo ""

# C
echo "Testing C..."
cd /Users/einand/Code/test/c
if gcc -o pi_test pi_test.c 2>/dev/null && ./pi_test 2>&1 | grep -E "All tests passed"; then
    echo "✓ C tests passed"
    ((total_passed++))
else
    echo "✗ C tests failed"
    ((total_failed++))
fi
((total_languages++))
echo ""

# C++
echo "Testing C++..."
cd /Users/einand/Code/test/cpp
if g++ -o pi_test pi_test.cpp -lgtest -lgtest_main -pthread 2>/dev/null && ./pi_test 2>&1 | grep -E "PASSED"; then
    echo "✓ C++ tests passed"
    ((total_passed++))
else
    echo "⊘ C++ tests skipped (Google Test not available)"
fi
((total_languages++))
echo ""

# Rust
echo "Testing Rust..."
cd /Users/einand/Code/test/rust
if cargo test 2>&1 | grep -E "test result: ok"; then
    echo "✓ Rust tests passed"
    ((total_passed++))
else
    echo "✗ Rust tests failed"
    ((total_failed++))
fi
((total_languages++))
echo ""

# Ruby
echo "Testing Ruby..."
cd /Users/einand/Code/test/ruby
if ruby test_pi.rb 2>&1 | grep -E "tests,.*assertions,.*failures,.*errors"; then
    echo "✓ Ruby tests passed"
    ((total_passed++))
else
    echo "✗ Ruby tests failed"
    ((total_failed++))
fi
((total_languages++))
echo ""

# PHP
echo "Testing PHP..."
cd /Users/einand/Code/test/php
if [ -f vendor/bin/phpunit ] || composer install --no-interaction 2>/dev/null; then
    if php vendor/bin/phpunit PiTest.php 2>&1 | grep -E "OK"; then
        echo "✓ PHP tests passed"
        ((total_passed++))
    else
        echo "✗ PHP tests failed"
        ((total_failed++))
    fi
else
    echo "⊘ PHP tests skipped (PHPUnit not available)"
fi
((total_languages++))
echo ""

# Swift
echo "Testing Swift..."
cd /Users/einand/Code/test/swift
if swift test 2>&1 | grep -E "Test Suite"; then
    echo "✓ Swift tests passed"
    ((total_passed++))
else
    echo "✗ Swift tests failed"
    ((total_failed++))
fi
((total_languages++))
echo ""

# D
echo "Testing D..."
cd /Users/einand/Code/test/d
if ldc2 -unittest -run pi_test.d 2>&1 | grep -E "All tests passed"; then
    echo "✓ D tests passed"
    ((total_passed++))
else
    echo "✗ D tests failed"
    ((total_failed++))
fi
((total_languages++))
echo ""

# Nim
echo "Testing Nim..."
cd /Users/einand/Code/test/nim
if nim c -r test_pi.py 2>&1 | grep -E "tests passed"; then
    echo "✓ Nim tests passed"
    ((total_passed++))
else
    echo "✗ Nim tests failed"
    ((total_failed++))
fi
((total_languages++))
echo ""

# Crystal
echo "Testing Crystal..."
cd /Users/einand/Code/test/crystal
if crystal spec spec/pi_spec.cr 2>&1 | grep -E "examples,.*failures"; then
    echo "✓ Crystal tests passed"
    ((total_passed++))
else
    echo "✗ Crystal tests failed"
    ((total_failed++))
fi
((total_languages++))
echo ""

# Haskell
echo "Testing Haskell..."
cd /Users/einand/Code/test/haskell
if ghc -o pi_test PiTest.hs 2>/dev/null && ./pi_test 2>&1 | grep -E "Cases:.*Tried:.*Errors: 0"; then
    echo "✓ Haskell tests passed"
    ((total_passed++))
else
    echo "⊘ Haskell tests skipped (HUnit not available)"
fi
((total_languages++))
echo ""

# Erlang
echo "Testing Erlang..."
cd /Users/einand/Code/test/erlang
if erlc pi_test.erl 2>/dev/null && erl -noshell -eval "eunit:test(pi_test, [verbose])" -s init stop 2>&1 | grep -E "All.*tests passed"; then
    echo "✓ Erlang tests passed"
    ((total_passed++))
else
    echo "✗ Erlang tests failed"
    ((total_failed++))
fi
((total_languages++))
echo ""

# Perl
echo "Testing Perl..."
cd /Users/einand/Code/test/perl
if perl test_pi.t 2>&1 | grep -E "All tests successful"; then
    echo "✓ Perl tests passed"
    ((total_passed++))
else
    echo "✗ Perl tests failed"
    ((total_failed++))
fi
((total_languages++))
echo ""

# Lua
echo "Testing Lua..."
cd /Users/einand/Code/test/lua
if busted spec/pi_spec.lua 2>&1 | grep -E "successes"; then
    echo "✓ Lua tests passed"
    ((total_passed++))
else
    echo "⊘ Lua tests skipped (Busted not available)"
fi
((total_languages++))
echo ""

# Zig
echo "Testing Zig..."
cd /Users/einand/Code/test/zig
if zig test pi_test.zig 2>&1 | grep -E "All.*tests passed"; then
    echo "✓ Zig tests passed"
    ((total_passed++))
else
    echo "✗ Zig tests failed"
    ((total_failed++))
fi
((total_languages++))
echo ""

# Odin
echo "Testing Odin..."
cd /Users/einand/Code/test/odin
if odin test pi_test.odin 2>&1 | grep -E "passed"; then
    echo "✓ Odin tests passed"
    ((total_passed++))
else
    echo "✗ Odin tests failed"
    ((total_failed++))
fi
((total_languages++))
echo ""

# C#
echo "Testing C#..."
cd /Users/einand/Code/test/csharp
if dotnet test 2>&1 | grep -E "Passed"; then
    echo "✓ C# tests passed"
    ((total_passed++))
else
    echo "✗ C# tests failed"
    ((total_failed++))
fi
((total_languages++))
echo ""

# Summary
echo "========================================="
echo "Summary"
echo "========================================="
echo "Languages tested: $total_languages"
echo "Tests passed: $total_passed"
echo "Tests failed: $total_failed"
echo "Tests skipped: $((total_languages - total_passed - total_failed))"
echo ""

if [ $total_failed -eq 0 ]; then
    echo "✓ All tests passed!"
    exit 0
else
    echo "✗ Some tests failed"
    exit 1
fi