#!/bin/bash
# scripts/enforce-ddd-boundaries.sh
# Enforces DDD bounded context dependency rules
#
# Exit codes:
#   0 = No boundary violations
#   1 = Boundary violations found (MUST FIX)
#
# Usage: ./scripts/enforce-ddd-boundaries.sh

set -e

echo "🚧 DDD Boundary Enforcement"
echo "==========================="
echo ""

# Define allowed cross-domain dependencies
# Format: "SourceDomain:TargetDomain1,TargetDomain2"
ALLOWED_DEPS=(
    "Ordering:Shared,Gala"        # Ordering can access Shared + Gala
    "Notifications:Shared"        # Notifications only accesses Shared
    "Gala:Shared"                 # Gala only accesses Shared
    "Venue:Shared"                # Venue only accesses Shared
    "IAM:Shared"                  # IAM only accesses Shared
)

echo "Allowed Dependencies:"
for DEP in "${ALLOWED_DEPS[@]}"; do
    echo "  - ${DEP//:/ → }"
done
echo ""

VIOLATIONS=0

# Check each domain's imports
for DOMAIN_DIR in app/Domains/*/; do
    DOMAIN=$(basename "$DOMAIN_DIR")

    # Skip Shared (everyone can access it)
    if [ "$DOMAIN" = "Shared" ]; then
        continue
    fi

    # Find all imports from other domains (exclude same domain and Shared)
    CROSS_DOMAIN_IMPORTS=$(grep -rh "^use App\\\\Domains\\\\" "$DOMAIN_DIR" 2>/dev/null | \
        grep -v "use App\\\\Domains\\\\${DOMAIN}\\\\" | \
        grep -v "use App\\\\Domains\\\\Shared\\\\" | \
        sed 's/use App\\Domains\\//' | \
        sed 's/\\.*//' | \
        cut -d'\\' -f1 | \
        sort -u || true)

    if [ -n "$CROSS_DOMAIN_IMPORTS" ]; then
        # Get allowed targets for this domain
        ALLOWED=""
        for DEP in "${ALLOWED_DEPS[@]}"; do
            if [[ "$DEP" == "$DOMAIN:"* ]]; then
                ALLOWED=$(echo "$DEP" | cut -d: -f2)
                break
            fi
        done

        # Check each imported domain
        while IFS= read -r IMPORTED_DOMAIN; do
            if [ -z "$IMPORTED_DOMAIN" ]; then
                continue
            fi

            # Check if this import is allowed
            if [[ -z "$ALLOWED" ]] || [[ ! ",$ALLOWED," =~ ",$IMPORTED_DOMAIN," ]]; then
                echo "❌ VIOLATION: $DOMAIN → $IMPORTED_DOMAIN (not allowed)"
                echo "   Files affected:"
                grep -r "use App\\\\Domains\\\\${IMPORTED_DOMAIN}" "$DOMAIN_DIR" 2>/dev/null | \
                    cut -d: -f1 | uniq | sed 's/^/     /'
                echo ""
                VIOLATIONS=$((VIOLATIONS + 1))
            else
                echo "✅ $DOMAIN → $IMPORTED_DOMAIN (allowed)"
            fi
        done <<< "$CROSS_DOMAIN_IMPORTS"
    else
        echo "✅ $DOMAIN has no cross-domain imports"
    fi
done

echo ""
echo "================================"
if [ $VIOLATIONS -gt 0 ]; then
    echo "❌ Found $VIOLATIONS boundary violation(s)"
    echo ""
    echo "To fix: Remove unauthorized cross-domain imports"
    echo "Or: Update ALLOWED_DEPS in this script if the dependency is intentional"
    exit 1
else
    echo "✅ No boundary violations found"
    exit 0
fi
