#!/bin/bash
# Fast verification for agent use (skips slow tests)
# Exit codes: 0 = PASS, 1 = FAIL

STORY_ID="$1"
DOMAIN="$2"

if [ -z "$STORY_ID" ]; then
    echo "Usage: ./scripts/verify-story-fast.sh GG-XXX [domain]"
    exit 1
fi

# Auto-detect domain
if [ -z "$DOMAIN" ]; then
    case "$STORY_ID" in
        GG-170|GG-171|GG-172) DOMAIN="Venue" ;;
        GG-173|GG-174|GG-175|GG-176|GG-177|GG-178|GG-179|GG-180) DOMAIN="Ordering" ;;
        GG-181|GG-182|GG-183|GG-184|GG-185|GG-186|GG-187|GG-188|GG-189) DOMAIN="Gala" ;;
        GG-190|GG-191|GG-192|GG-193|GG-194|GG-195|GG-196|GG-197|GG-198|GG-199|GG-200|GG-201) DOMAIN="Notifications" ;;
        *) echo "❌ Unknown story $STORY_ID"; exit 1 ;;
    esac
fi

echo "Verifying: $STORY_ID ($DOMAIN)"
FAILED=0

# 1. Syntax check (fast)
echo -n "[1/3] Syntax... "
if find "app/Domains/$DOMAIN" -name "*.php" -exec php -l {} \; >/dev/null 2>&1; then
    echo "✓"
else
    echo "❌ FAIL"
    find "app/Domains/$DOMAIN" -name "*.php" -exec php -l {} \; 2>&1 | grep "Parse error" | head -3
    FAILED=1
fi

# 2. Story-specific checks
echo -n "[2/3] Story AC... "

case "$STORY_ID" in
    GG-173)
        # Check broader magic string patterns
        MAGIC_COUNT=$(grep -rE "'status'[[:space:]]*=>[[:space:]]*['\"][a-z_]+['\"]" app/Domains/Ordering/Services --include="*.php" | grep -v "//" | wc -l | xargs)
        if [ "$MAGIC_COUNT" -gt 0 ]; then
            echo "❌ FAIL: $MAGIC_COUNT magic strings remain"
            grep -rn -E "'status'[[:space:]]*=>[[:space:]]*['\"][a-z_]+['\"]" app/Domains/Ordering/Services --include="*.php" | grep -v "//" | head -5
            FAILED=1
        else
            echo "✓"
        fi
        ;;

    GG-174)
        # Check DTOs exist
        if [ -f "app/Domains/Ordering/DTOs/OrderDTO.php" ]; then
            echo "✓"
        else
            echo "❌ FAIL: OrderDTO not found"
            FAILED=1
        fi
        ;;

    GG-179)
        # Check service split
        if [ -f "app/Domains/Ordering/Services/Payment/RevolutPaymentGateway.php" ]; then
            LINES=$(wc -l < app/Domains/Ordering/Services/Payment/RevolutPaymentGateway.php | xargs)
            if [ "$LINES" -lt 500 ]; then
                echo "✓ ($LINES lines)"
            else
                echo "❌ FAIL: Still $LINES lines (need <500)"
                FAILED=1
            fi
        fi
        ;;

    GG-181)
        # GalaForkService split
        if [ -f "app/Domains/Gala/Services/GalaForkService.php" ]; then
            LINES=$(wc -l < app/Domains/Gala/Services/GalaForkService.php | xargs)
            if [ "$LINES" -lt 300 ]; then
                echo "✓ ($LINES lines)"
            else
                echo "❌ FAIL: Still $LINES lines (need <300)"
                FAILED=1
            fi
        fi
        ;;

    *)
        echo "⚠ No specific checks for $STORY_ID"
        ;;
esac

# 3. Unit tests only (fast, focused on code changes)
echo -n "[3/3] Unit Tests... "
if vendor/bin/phpunit --filter="$DOMAIN" --testsuite=Unit --stop-on-failure --no-coverage >/dev/null 2>&1; then
    echo "✓"
else
    echo "❌ FAIL"
    vendor/bin/phpunit --filter="$DOMAIN" --testsuite=Unit --stop-on-failure --testdox 2>&1 | grep "✘" | head -5
    FAILED=1
fi

# Result
if [ $FAILED -eq 0 ]; then
    echo "✅ PASS: $STORY_ID verified"
    exit 0
else
    echo "❌ FAIL: Fix issues and re-run"
    exit 1
fi
