#!/bin/bash

echo "🔧 Setting up email configuration for User Auth Service"
echo "======================================================"

# Check if .env file exists
if [ -f ".env" ]; then
    echo "✅ .env file already exists"
    echo "📧 Current email configuration:"
    grep -E "^(SMTP_|GMAIL_|EMAIL_)" .env 2>/dev/null || echo "   No email configuration found"
else
    echo "📝 Creating .env file from template..."
    cp env.example .env
    echo "✅ .env file created from env.example"
fi

echo ""
echo "📧 Email Configuration Options:"
echo "1. Gmail (Recommended for development)"
echo "2. Custom SMTP Server"
echo "3. Skip email setup (use mock emails)"

read -p "Choose an option (1-3): " choice

case $choice in
    1)
        echo ""
        echo "📧 Gmail Configuration"
        echo "====================="
        echo "You'll need to create an App Password for your Gmail account:"
        echo "1. Go to https://myaccount.google.com/security"
        echo "2. Enable 2-Step Verification if not already enabled"
        echo "3. Go to 'App passwords' and generate a new app password"
        echo "4. Use that password below (not your regular Gmail password)"
        echo ""
        
        read -p "Enter your Gmail address: " gmail_user
        read -s -p "Enter your Gmail App Password: " gmail_pass
        echo ""
        
        # Update .env file
        sed -i.bak "s/GMAIL_USER=.*/GMAIL_USER=$gmail_user/" .env
        sed -i.bak "s/GMAIL_APP_PASSWORD=.*/GMAIL_APP_PASSWORD=$gmail_pass/" .env
        sed -i.bak "s/SMTP_FROM=.*/SMTP_FROM=$gmail_user/" .env
        
        echo "✅ Gmail configuration updated in .env file"
        ;;
    2)
        echo ""
        echo "📧 Custom SMTP Configuration"
        echo "============================"
        
        read -p "Enter SMTP host (e.g., smtp.gmail.com): " smtp_host
        read -p "Enter SMTP port (e.g., 587): " smtp_port
        read -p "Enter SMTP username/email: " smtp_user
        read -s -p "Enter SMTP password: " smtp_pass
        echo ""
        read -p "Enter from email address: " smtp_from
        
        # Update .env file
        sed -i.bak "s/SMTP_HOST=.*/SMTP_HOST=$smtp_host/" .env
        sed -i.bak "s/SMTP_PORT=.*/SMTP_PORT=$smtp_port/" .env
        sed -i.bak "s/SMTP_USER=.*/SMTP_USER=$smtp_user/" .env
        sed -i.bak "s/SMTP_PASS=.*/SMTP_PASS=$smtp_pass/" .env
        sed -i.bak "s/SMTP_FROM=.*/SMTP_FROM=$smtp_from/" .env
        
        echo "✅ SMTP configuration updated in .env file"
        ;;
    3)
        echo ""
        echo "⚠️  Email setup skipped. Mock emails will be used in development."
        echo "   Users will still be able to register and login, but verification emails won't be sent."
        ;;
    *)
        echo "❌ Invalid option. Email setup skipped."
        ;;
esac

echo ""
echo "🔧 Next steps:"
echo "1. Review and edit .env file if needed"
echo "2. Restart the user-auth service: docker-compose restart user-auth"
echo "3. Check logs for email configuration status"
echo ""
echo "📧 To test email configuration:"
echo "   docker-compose logs user-auth | grep -E '(Email|SMTP|Gmail)'"
echo ""
echo "✅ Email setup complete!"
