"""
EN: Main Flask application entry point for the BBW University Research Platform.
    - Configures Flask app, mail, CORS, and resource management.
    - Registers blueprints for API and email handling.
    - Loads resources (materials, printers, qualities) from CSV files.
    - Renders the main HTML template.
DE: Haupteinstiegspunkt der Flask-Anwendung für die Forschungsplattform der bbw Hochschule.
    - Konfiguriert Flask-App, Mail, CORS und Ressourcenverwaltung.
    - Registriert Blueprints für API und E-Mail-Verarbeitung.
    - Lädt Ressourcen (Materialien, Drucker, Qualitäten) aus CSV-Dateien.
    - Rendert das Haupt-HTML-Template.
"""

import os
from flask import Flask, render_template
from flask_cors import CORS
from flask_mail import Mail
from app.services.ressourcen_manager import RessourcenManager

# Load environment variables from .env if present
try:
    from dotenv import load_dotenv
    load_dotenv(os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env'))
except ImportError:
    pass


app = Flask(__name__, static_folder='static', template_folder='templates')



# EN: Mail server configuration for contact form emails.
#     To enable email sending, set the following values:
#     - MAIL_USERNAME: The Gmail address used to send emails (e.g. your_gmail@gmail.com)
#     - MAIL_PASSWORD: The Gmail App Password (not your normal password!)
#     - MAIL_DEFAULT_SENDER: The same Gmail address as above
#     - MAIL_RECEIVER: The email address that will receive the contact form messages
#     For Gmail, you must use an App Password and have 2FA enabled. See:
#     https://support.google.com/accounts/answer/185833
# DE: Mailserver-Konfiguration für das Kontaktformular.
#     Um den Versand zu aktivieren, tragen Sie Folgendes ein:
#     - MAIL_USERNAME: Gmail-Adresse zum Senden (z.B. ihr_gmail@gmail.com)
#     - MAIL_PASSWORD: Gmail-App-Passwort (nicht das normale Passwort!)
#     - MAIL_DEFAULT_SENDER: Die gleiche Gmail-Adresse wie oben
#     - MAIL_RECEIVER: Zieladresse für Kontaktformulare
#     Für Gmail benötigen Sie ein App-Passwort und aktivierte 2FA. Siehe Link oben.
# DE: Mailserver-Konfiguration für das Kontaktformular.

# === MAIL CONFIGURATION ===
# All sensitive data is now loaded from the .env file in the project root.
# Edit .env to set your credentials and receiver email.
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.getenv('MAIL_USERNAME', '')
app.config['MAIL_PASSWORD'] = os.getenv('MAIL_PASSWORD', '')
app.config['MAIL_DEFAULT_SENDER'] = os.getenv('MAIL_DEFAULT_SENDER', '')

MAIL_RECEIVER = os.getenv('MAIL_RECEIVER', '')

mail = Mail(app)
CORS(app)

from app.send_email import send_email_bp
app.register_blueprint(send_email_bp)
app.mail = mail
app.mail_receiver = MAIL_RECEIVER


# EN/DE: Load resource CSV paths.
basis_pfad = os.path.dirname(os.path.abspath(__file__))
material_pfad = os.path.join(basis_pfad, "daten", "materialien.csv")
qualitaet_pfad = os.path.join(basis_pfad, "daten", "qualitaeten.csv")
drucker_pfad = os.path.join(basis_pfad, "daten", "drucker.csv")

ressourcen = RessourcenManager()
ressourcen.lade_alle_ressourcen(
    material_daten_pfad=material_pfad,
    qualitaet_daten_pfad=qualitaet_pfad,
    drucker_daten_pfad=drucker_pfad
)

app.ressourcen = ressourcen

from app.api.routes import api_blueprint
app.register_blueprint(api_blueprint, url_prefix='/api')


@app.route('/')
def index():
    """
    EN: Render the main landing page (index.html).
    DE: Rendert die Hauptstartseite (index.html).
    """
    return render_template('index.html')

if __name__ == '__main__':
    # EN/DE: Start Flask development server.
    app.run(debug=True)