from flask import Blueprint, flash, redirect, render_template, url_for
from flask_login import current_user, login_required
from flask_mailman import EmailMessage
from sqlalchemy import select

from web_application import db
from web_application.models import Bolum

ana = Blueprint(
    "ana",
    __name__,
    template_folder="templates",
    static_folder="static",
)


@ana.route("/")
@ana.route("/index")
@login_required
def index():
    bolumler = db.session.execute(select(Bolum)).scalars().all()
    data = {
        "title": "Üretim Bölümler",
        "subtitle": "Fabrika genelindeki aktif üretim hatları ve departmanlar",
        "bolumler": bolumler,
    }
    return render_template("ana/index.html", **data)


@ana.route("/mail")
def mail():
    print("Sending email...")
    msg = EmailMessage(
        subject="Hello",
        body="This is a test email.",
        from_email="donotreply@scholars.tr",
        to=["dinceraydic@gmail.com"],
    )
    res = msg.send()
    print(res)
    return {"status": "Email sent successfully!"}, 200
