#!/bin/bash
########################################
# Test BCC Fix - Local Verification
# Tests that BCC archiving works correctly
########################################

set -e

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

cd "$PROJECT_ROOT" || exit 1

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " 🧪 BCC FIX VERIFICATION TEST"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# Step 1: Verify config
echo "1️⃣  Checking BCC configuration..."
BCC_CONFIG=$(php artisan tinker --execute="echo config('mail.bcc_archive');")
echo "   BCC Config: $BCC_CONFIG"

if [ -z "$BCC_CONFIG" ]; then
    echo "   ❌ FAIL: BCC config is empty"
    exit 1
fi

echo "   ✅ PASS: BCC config is set"
echo ""

# Step 2: Check if queue worker is running
echo "2️⃣  Checking queue worker status..."
if ps aux | grep "queue:work" | grep -v grep > /dev/null; then
    echo "   ✅ PASS: Queue worker is running"
else
    echo "   ⚠️  WARNING: Queue worker not running"
    echo "   Starting queue worker in background..."
    php artisan queue:work --tries=3 --timeout=90 > /dev/null 2>&1 &
    QUEUE_PID=$!
    echo "   Started queue worker (PID: $QUEUE_PID)"
    sleep 2
fi
echo ""

# Step 3: Send test email
echo "3️⃣  Sending test email..."
TEST_EMAIL="charlie+bcctest$(date +%s)@09-07.xyz"

php artisan tinker --execute="
\$user = new \App\User();
\$user->email = '$TEST_EMAIL';
\$user->first_name = 'BCC Test';
\$user->last_name = 'User';
\$user->save();
Mail::to('$TEST_EMAIL')->queue(new \App\Mail\WelcomeUserMail(\$user));
echo 'Email queued successfully';
" 2>&1

echo "   ✅ Email queued"
echo ""

# Step 4: Wait for queue to process
echo "4️⃣  Waiting for queue to process email (30 seconds)..."
for i in {1..30}; do
    echo -n "."
    sleep 1
done
echo ""
echo "   ✅ Queue processing complete"
echo ""

# Step 5: Check email logs
echo "5️⃣  Checking Laravel logs for BCC confirmation..."
LOG_FILE="storage/logs/laravel-$(date +%Y-%m-%d).log"

if grep -q "BCC archive added to email" "$LOG_FILE"; then
    echo "   ✅ PASS: BCC log entry found"
    grep "BCC archive added to email" "$LOG_FILE" | tail -1
else
    echo "   ⚠️  WARNING: No BCC log entry (check log level is DEBUG)"
fi
echo ""

# Step 6: Mailpit instructions
echo "6️⃣  Manual verification in Mailpit:"
echo "   📧 Open: http://localhost:8025"
echo "   🔍 Find email to: $TEST_EMAIL"
echo "   ✅ Verify BCC header shows: $BCC_CONFIG"
echo ""

# Step 7: Check email_logs database
echo "7️⃣  Checking email_logs database..."
php artisan tinker --execute="
\$recent = \DB::table('email_logs')
    ->where('recipient_email', '$TEST_EMAIL')
    ->orderBy('created_at', 'desc')
    ->first();

if (\$recent) {
    echo '   ✅ Email logged in database';
} else {
    echo '   ⚠️  Email not in database yet (may still be queued)';
}
" 2>&1
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " ✅ TEST COMPLETE"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "🔍 NEXT STEPS:"
echo "   1. Check Mailpit (http://localhost:8025)"
echo "   2. Look for email to: $TEST_EMAIL"
echo "   3. Verify BCC header shows: $BCC_CONFIG"
echo "   4. If BCC is present → ✅ FIX WORKS!"
echo "   5. If BCC is missing → ❌ FIX FAILED"
echo ""
