#!/usr/bin/env bash
DBPASS=$(grep ^DB_PASSWORD /var/www/html/mathapi/.env | cut -d= -f2-)
cd /var/www/html/vocabapi

echo "=== prod .env MAIL_* (redacted) ==="
grep -E "^MAIL_" .env | sed -E 's/(MAIL_PASSWORD)=.*/\1=<set>/'

echo ""
echo "=== configs table rows with mail_ ==="
mysql -uroot -p"$DBPASS" vocab -e "SELECT id, school_id, \`key\`, value FROM configs WHERE \`key\` LIKE 'mail_%';" 2>/dev/null

echo ""
echo "=== Runtime mail config (after AppServiceProvider::boot) ==="
php artisan tinker --execute='echo "host=".config("mail.mailers.smtp.host"), PHP_EOL, "port=".config("mail.mailers.smtp.port"), PHP_EOL, "enc=".config("mail.mailers.smtp.encryption"), PHP_EOL, "user=".config("mail.mailers.smtp.username"), PHP_EOL, "pass=".(config("mail.mailers.smtp.password") ? "<set>" : "<unset>"), PHP_EOL, "from=".config("mail.from.address"), PHP_EOL;' 2>&1

echo ""
echo "=== TCP probe: can server reach mail.privateemail.com:465? ==="
timeout 5 bash -c "</dev/tcp/mail.privateemail.com/465" 2>&1 && echo "privateemail:465 OPEN" || echo "privateemail:465 BLOCKED"
echo ""
echo "=== TCP probe: can server reach smtp.gmail.com:587? ==="
timeout 5 bash -c "</dev/tcp/smtp.gmail.com/587" 2>&1 && echo "gmail:587 OPEN" || echo "gmail:587 BLOCKED"

echo ""
echo "=== Laravel log tail (just the most recent OTP attempt error if any) ==="
tail -n 100 storage/logs/laravel.log | grep -E "Mail|SMTP|Symfony.*Mailer|Connection|swiftmailer|stream_socket" | tail -20

echo ""
echo "=== Live mail-send test (forces re-send with current config) ==="
php artisan tinker --execute='try { Mail::raw("vocab prod mail test ".now(), function($m) { $m->to("pamelaliusm@gmail.com")->subject("Vocab prod mail test"); }); echo "SENT OK\n"; } catch (\Throwable $e) { echo "FAIL: ".$e->getMessage()."\n".$e->getFile().":".$e->getLine()."\n"; }' 2>&1
