"""
EN: Handles email sending for the contact form in the BBW University Research Platform.
    - Defines a Flask Blueprint for the /send_email endpoint.
    - Uses Flask-Mail to send emails to the configured recipient.
    - IMPORTANT: The sender and receiver email addresses are configured in main.py (see comments there).
    - To enable email sending, you must set:
        - app.config['MAIL_USERNAME'] = 'your_gmail_account@gmail.com'  # The Gmail account used to send emails
        - app.config['MAIL_PASSWORD'] = 'your_gmail_app_password'      # The app password for that Gmail account
        - app.config['MAIL_DEFAULT_SENDER'] = 'your_gmail_account@gmail.com'  # Sender address (should match username)
        - MAIL_RECEIVER = 'destination_email@example.com'              # The email address that will receive the contact form messages
    - For Gmail, you must use an App Password (not your normal password) and have 2FA enabled.
    - More info: https://support.google.com/accounts/answer/185833
DE: Verantwortlich für das Versenden von E-Mails aus dem Kontaktformular der Forschungsplattform der bbw Hochschule.
    - Definiert einen Flask-Blueprint für den Endpunkt /send_email.
    - Verwendet Flask-Mail zum Senden von E-Mails an den konfigurierten Empfänger.
    - WICHTIG: Die Absender- und Empfängeradressen werden in main.py konfiguriert (siehe Kommentare dort).
    - Um den E-Mail-Versand zu aktivieren, müssen Sie:
        - app.config['MAIL_USERNAME'] = 'ihr_gmail_konto@gmail.com'    # Gmail-Konto zum Senden
        - app.config['MAIL_PASSWORD'] = 'ihr_gmail_app_passwort'       # App-Passwort für dieses Konto
        - app.config['MAIL_DEFAULT_SENDER'] = 'ihr_gmail_konto@gmail.com' # Absenderadresse (sollte mit Username übereinstimmen)
        - MAIL_RECEIVER = 'zieladresse@example.com'                   # Zieladresse für Kontaktformulare
    - Für Gmail benötigen Sie ein App-Passwort (nicht Ihr normales Passwort) und aktivierte 2FA.
    - Mehr Infos: https://support.google.com/accounts/answer/185833
"""
import os
from flask import Blueprint, request, redirect, url_for, current_app
from flask_mail import Mail, Message

send_email_bp = Blueprint('send_email', __name__)

@send_email_bp.route('/send_email', methods=['POST'])
def sende_email():
    """
    EN: Handles the sending of contact emails from the web form.
    DE: Verarbeitet das Senden von Kontakt-E-Mails aus dem Webformular.
    """
    vorname = request.form.get('name')  # EN: Sender's name / DE: Name des Absenders
    email = request.form.get('email')   # EN: Sender's email / DE: E-Mail des Absenders
    betreff = request.form.get('betreff') # EN: Subject / DE: Betreff
    nachricht = request.form.get('nachricht') # EN: Message / DE: Nachricht
    if not all([vorname, email, betreff, nachricht]):
        return redirect(url_for('index'))
    empfaenger = current_app.mail_receiver # EN: Recipient / DE: Empfänger
    msg = Message(f"Kontakt: {betreff}",
                  sender=current_app.config['MAIL_DEFAULT_SENDER'],
                  recipients=[empfaenger])
    msg.body = f"Name: {vorname}\nE-Mail: {email}\nNachricht:\n{nachricht}"
    current_app.mail.send(msg)
    return redirect(url_for('index', success='true'))
