#!/bin/bash
# =============================================================================
# Bounded Context Isolation Check
# Ensures Domain layer has no framework dependencies
# =============================================================================

set -e

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

ERRORS=0

echo "Checking Bounded Context isolation..."
echo ""

# Define forbidden imports in Domain layer
FORBIDDEN_PATTERNS=(
    "use Symfony\\\\"
    "use Doctrine\\\\"
    "use ApiPlatform\\\\"
    "use Lexik\\\\"
    "#\[ORM\\\\"
    "#\[ApiResource"
    "#\[Assert\\\\"
)

# Check each BC Domain folder
for BC in Administration Scolarite VieScolaire Communication; do
    DOMAIN_PATH="backend/src/${BC}/Domain"

    if [ -d "$DOMAIN_PATH" ]; then
        echo "Checking ${BC}/Domain..."

        for pattern in "${FORBIDDEN_PATTERNS[@]}"; do
            # Use grep with -r for recursive, -n for line numbers
            if grep -rn "$pattern" "$DOMAIN_PATH" 2>/dev/null; then
                echo -e "${RED}ERROR: Found forbidden import pattern '$pattern' in ${BC}/Domain${NC}"
                ERRORS=$((ERRORS + 1))
            fi
        done
    fi
done

# Check Shared/Domain as well
SHARED_DOMAIN="backend/src/Shared/Domain"
if [ -d "$SHARED_DOMAIN" ]; then
    echo "Checking Shared/Domain..."

    for pattern in "${FORBIDDEN_PATTERNS[@]}"; do
        if grep -rn "$pattern" "$SHARED_DOMAIN" 2>/dev/null; then
            echo -e "${RED}ERROR: Found forbidden import pattern '$pattern' in Shared/Domain${NC}"
            ERRORS=$((ERRORS + 1))
        fi
    done
fi

echo ""
if [ $ERRORS -gt 0 ]; then
    echo -e "${RED}BC Isolation check FAILED with $ERRORS error(s)${NC}"
    echo "Domain layer must be pure PHP without framework dependencies."
    exit 1
else
    echo -e "${GREEN}BC Isolation check PASSED${NC}"
    exit 0
fi
