#!/bin/bash

# Centraliserad test för alla pi-beräkningar
# Användning:
#   ./test.sh           - Kör alla tester
#   ./test.sh bash      - Kör bara bash-tester
#   ./test.sh python    - Kör bara python-tester
#   etc.

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

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

# Alla mappar att testa
ALL_FOLDERS="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"

# Om argument ges, testa bara det språket
if [ $# -gt 0 ]; then
    FOLDERS="$1"
    # Kontrollera att språket finns
    if [[ ! " $ALL_FOLDERS " =~ " $FOLDERS " ]]; then
        echo -e "${RED}Fel: Okänt språk '$FOLDERS'${NC}"
        echo "Tillgängliga språk: $ALL_FOLDERS"
        exit 1
    fi
else
    FOLDERS="$ALL_FOLDERS"
fi

total_tests=0
passed_tests=0
failed_tests=0

echo "=== Centraliserad Pi-beräkning Test ==="
if [ $# -gt 0 ]; then
    echo "Testar bara: $FOLDERS"
fi
echo ""

# Loopa igenom alla mappar
for folder in $FOLDERS; do
    if [ ! -d "$folder" ]; then
        echo -e "${RED}✗ $folder: Mappen finns inte${NC}"
        continue
    fi
    
    # Check if binary exists
    if [ ! -f "$folder/bin/print_hej" ]; then
        echo -e "${YELLOW}⊘ $folder: Binären finns inte (kör ./build.sh först)${NC}"
        continue
    fi

    echo "Testar $folder..."
    cd "$folder"

    lang_passed=0
    lang_failed=0

    # Bestäm kommando baserat på mapp
    case "$folder" in
        bash)      CMD="bin/print_hej" ;;
        brainfuck) CMD="bin/print_hej" ;;
        java)      CMD="bin/print_hej" ;;
        javascript) CMD="bin/print_hej" ;;
        typescript) CMD="bin/print_hej" ;;
        lua)       CMD="bin/print_hej" ;;
        perl)      CMD="bin/print_hej" ;;
        php)       CMD="bin/print_hej" ;;
        python)    CMD="bin/print_hej" ;;
        ruby)      CMD="bin/print_hej" ;;
        erlang)    CMD="bin/print_hej" ;;
        *)         CMD="bin/print_hej" ;;
    esac

    # Testa varje decimal-värde
    for decimals in 1 2 5 10 100 1000 10000; do
        # Hämta förväntat värde från facit.txt
        expected=$(head -c $((decimals + 2)) ../facit.txt)

        # Kör programmet med timeout (öka timeout för stora decimaler)
        timeout=10
        if [ "$decimals" -gt 1000 ]; then
            timeout=60
        fi
        result=$(gtimeout $timeout bash -c "$CMD $decimals 2>/dev/null || echo TIMEOUT")

        # Kontrollera resultat
        if [ "$result" = "TIMEOUT" ]; then
            echo -e "  ${RED}✗${NC} $decimals decimaler: TIMEOUT"
            ((lang_failed++))
        elif [ "$result" = "$expected" ]; then
            echo -e "  ${GREEN}✓${NC} $decimals decimaler"
            ((lang_passed++))
        else
            echo -e "  ${RED}✗${NC} $decimals decimaler: Fel resultat"
            ((lang_failed++))
        fi

        ((total_tests++))
    done

    cd ..

    if [ $lang_failed -eq 0 ]; then
        ((passed_tests+=7))
        echo -e "${GREEN}✓ $folder: Alla tester passerade${NC}"
    else
        ((failed_tests+=lang_failed))
        ((passed_tests+=lang_passed))
        echo -e "${RED}✗ $folder: $lang_failed tester misslyckades${NC}"
    fi
    echo ""
done

echo "=== Sammanfattning ==="
echo "Totalt: $total_tests tester"
echo -e "Passerade: ${GREEN}$passed_tests${NC}"
echo -e "Misslyckade: ${RED}$failed_tests${NC}"

if [ $failed_tests -eq 0 ]; then
    echo -e "${GREEN}✓ Alla tester passerade!${NC}"
    exit 0
else
    echo -e "${RED}✗ Några tester misslyckades${NC}"
    exit 1
fi
