#!/bin/bash

# Script pour exécuter TOUS les tests unitaires du projet

echo "========================================"
echo "Tests Booking Platform - Suite Complète"
echo "========================================"
echo ""

# Couleurs
GREEN='\033[0;32m'
RED='\033[0;31m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

FAILED=0
PASSED=0
TOTAL=0

# Fonction pour exécuter un fichier de test
run_test() {
    local file=$1
    local name=$(basename "$file" .php)
    
    TOTAL=$((TOTAL + 1))
    
    echo -e "${BLUE}[$TOTAL] Testing: $name${NC}"
    php vendor/bin/phpunit "$file" --colors=never 2>&1 | tail -3
    
    if [ ${PIPESTATUS[0]} -eq 0 ]; then
        echo -e "${GREEN}✓ $name: PASSED${NC}"
        PASSED=$((PASSED + 1))
    else
        echo -e "${RED}✗ $name: FAILED${NC}"
        FAILED=$((FAILED + 1))
    fi
    echo ""
}

echo -e "${YELLOW}Running all non-DB tests (discovered automatically)${NC}"
echo "--------------------------------------------------------"

# Discover all test files under tests/ excluding *DbTest.php (DB tests run separately)
TEST_FILES=()
while IFS= read -r tf; do
    TEST_FILES+=("$tf")
done < <(find tests -type f -name '*Test.php' ! -name '*DbTest.php' | sort)

# Exclude tests that are run in phpunit-db.xml to avoid running DB tests twice.
DB_TEST_FILES=()
if [ -f phpunit-db.xml ]; then
    while IFS= read -r f; do
        # normalize relative paths
        DB_TEST_FILES+=("$f")
    done < <(sed -n 's:.*<file>\(.*\)</file>.*:\1:p' phpunit-db.xml)

    # filter TEST_FILES to remove any that appear in DB_TEST_FILES
    FILTERED=()
    for tf in "${TEST_FILES[@]}"; do
        skip=0
        for dbf in "${DB_TEST_FILES[@]}"; do
            # compare basenames to match entries like tests/Foo.php
            if [ "$(realpath --relative-to="$(pwd)" "$tf")" = "$dbf" ] || [ "$(basename "$tf")" = "$(basename "$dbf")" ]; then
                skip=1
                break
            fi
        done
        if [ $skip -eq 0 ]; then
            FILTERED+=("$tf")
        fi
    done
    TEST_FILES=("${FILTERED[@]}")
fi

for tf in "${TEST_FILES[@]}"; do
    run_test "$tf"
done

echo ""
echo -e "${YELLOW}Now running DB tests via phpunit-db.xml${NC}"
echo "--------------------------------------------------------"
# Run DB tests as a single phpunit invocation using db config
TOTAL=$((TOTAL + 1))
php vendor/bin/phpunit --configuration phpunit-db.xml --colors=never 2>&1 | tail -3
if [ ${PIPESTATUS[0]} -eq 0 ]; then
    echo -e "${GREEN}✓ DB tests: PASSED${NC}"
    PASSED=$((PASSED + 1))
else
    echo -e "${RED}✗ DB tests: FAILED${NC}"
    FAILED=$((FAILED + 1))
fi

echo ""
echo "========================================"
echo "Résumé Final"
echo "========================================"
echo "Total: $TOTAL suites de tests"
echo -e "${GREEN}Réussis: $PASSED${NC}"
if [ $FAILED -gt 0 ]; then
    echo -e "${RED}Échecs: $FAILED${NC}"
fi
echo ""

if [ $FAILED -eq 0 ]; then
    echo -e "${GREEN}✓✓✓ Tous les tests sont passés ! ✓✓✓${NC}"
    exit 0
else
    echo -e "${RED}✗ $FAILED test(s) ont échoué${NC}"
    echo "Relancez les tests échoués individuellement pour plus de détails:"
    echo "  php vendor/bin/phpunit tests/NomDuTest.php --colors=always"
    exit 1
fi
