#!/bin/bash
#
# CUST-1.7: Verify User model namespace migration
#
# This script verifies that:
# 1. No old 'use App\User;' imports remain
# 2. No '\App\User::' fully-qualified references remain
# 3. app/User.php has been deleted
# 4. All tests pass
#
# Usage: ./scripts/verify-user-imports.sh

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

echo "=============================================="
echo "CUST-1.7: User Model Namespace Verification"
echo "=============================================="
echo ""

ERRORS=0

# Check 1: No 'use App\User;' imports
echo "[CHECK 1] Verifying no 'use App\\User;' imports remain..."
OLD_IMPORTS=$(grep -r "use App\\\\User;" "$PROJECT_ROOT/app/" "$PROJECT_ROOT/tests/" "$PROJECT_ROOT/config/" "$PROJECT_ROOT/database/" --include="*.php" 2>/dev/null | wc -l | tr -d ' ')

if [ "$OLD_IMPORTS" -gt 0 ]; then
    echo "  [FAIL] Found $OLD_IMPORTS files with old imports:"
    grep -r "use App\\\\User;" "$PROJECT_ROOT/app/" "$PROJECT_ROOT/tests/" "$PROJECT_ROOT/config/" "$PROJECT_ROOT/database/" --include="*.php" 2>/dev/null || true
    ERRORS=$((ERRORS + 1))
else
    echo "  [PASS] No old 'use App\\User;' imports found"
fi
echo ""

# Check 2: No '\App\User::' fully-qualified references
echo "[CHECK 2] Verifying no '\\App\\User::' references remain..."
OLD_FQ_REFS=$(grep -r "\\\\App\\\\User::" "$PROJECT_ROOT/app/" "$PROJECT_ROOT/tests/" "$PROJECT_ROOT/config/" "$PROJECT_ROOT/database/" --include="*.php" 2>/dev/null | wc -l | tr -d ' ')

if [ "$OLD_FQ_REFS" -gt 0 ]; then
    echo "  [FAIL] Found $OLD_FQ_REFS files with old fully-qualified references:"
    grep -r "\\\\App\\\\User::" "$PROJECT_ROOT/app/" "$PROJECT_ROOT/tests/" "$PROJECT_ROOT/config/" "$PROJECT_ROOT/database/" --include="*.php" 2>/dev/null || true
    ERRORS=$((ERRORS + 1))
else
    echo "  [PASS] No old '\\App\\User::' references found"
fi
echo ""

# Check 3: No 'App\User::class' in config files (without backslash prefix)
echo "[CHECK 3] Verifying no 'App\\User::class' in config files..."
CONFIG_REFS=$(grep -r "App\\\\User::class" "$PROJECT_ROOT/config/" --include="*.php" 2>/dev/null | grep -v "App\\\\Domains\\\\Shared\\\\Models\\\\User::class" | wc -l | tr -d ' ')

if [ "$CONFIG_REFS" -gt 0 ]; then
    echo "  [FAIL] Found old App\\User::class in config files:"
    grep -r "App\\\\User::class" "$PROJECT_ROOT/config/" --include="*.php" 2>/dev/null | grep -v "App\\\\Domains\\\\Shared\\\\Models\\\\User::class" || true
    ERRORS=$((ERRORS + 1))
else
    echo "  [PASS] No old 'App\\User::class' in config files"
fi
echo ""

# Check 4: app/User.php deleted
echo "[CHECK 4] Verifying app/User.php has been deleted..."
if [ -f "$PROJECT_ROOT/app/User.php" ]; then
    echo "  [FAIL] app/User.php still exists!"
    ERRORS=$((ERRORS + 1))
else
    echo "  [PASS] app/User.php has been deleted"
fi
echo ""

# Check 5: New User model exists in Shared kernel
echo "[CHECK 5] Verifying new User model exists..."
if [ -f "$PROJECT_ROOT/app/Domains/Shared/Models/User.php" ]; then
    echo "  [PASS] App\\Domains\\Shared\\Models\\User exists"
else
    echo "  [FAIL] App\\Domains\\Shared\\Models\\User does not exist!"
    ERRORS=$((ERRORS + 1))
fi
echo ""

# Check 6: config/auth.php uses new namespace
echo "[CHECK 6] Verifying config/auth.php uses new namespace..."
if grep -q "App\\\\Domains\\\\Shared\\\\Models\\\\User::class" "$PROJECT_ROOT/config/auth.php"; then
    echo "  [PASS] config/auth.php uses new namespace"
else
    echo "  [FAIL] config/auth.php does not use new namespace!"
    ERRORS=$((ERRORS + 1))
fi
echo ""

# Check 7: User alias removed from config/app.php
echo "[CHECK 7] Verifying User alias removed from config/app.php..."
if grep -q "'User' => App\\\\Domains\\\\Shared\\\\Models\\\\User::class" "$PROJECT_ROOT/config/app.php"; then
    echo "  [FAIL] User alias still exists in config/app.php!"
    ERRORS=$((ERRORS + 1))
else
    echo "  [PASS] User alias removed from config/app.php"
fi
echo ""

# Summary
echo "=============================================="
if [ "$ERRORS" -gt 0 ]; then
    echo "VERIFICATION FAILED: $ERRORS errors found"
    echo "=============================================="
    exit 1
else
    echo "VERIFICATION PASSED: All checks passed!"
    echo "=============================================="
    echo ""
    echo "Next step: Run full test suite"
    echo "  vendor/bin/phpunit"
    exit 0
fi
