from flask import Blueprint, redirect, url_for, render_template
from application.auth.forms import login_form
from application.auth.models import User
from flask_login import login_user, login_required, logout_user

# from application import keycloak

auth_blueprint = Blueprint("auth", __name__, template_folder="templates")


# Login as user or admin
@auth_blueprint.route("/login", methods=["GET", "POST"])
def login():
    loginForm = login_form()
    if loginForm.validate_on_submit():
        print("Test2")
        username = loginForm.username.data
        password = loginForm.password.data
        user = User.query.filter_by(username=username).first()

        if user and user.check_password(password=password):
            login_user(user)
            return redirect(url_for("dash.dashboard"))
    return render_template("login.html", form=loginForm)


@auth_blueprint.route("/logout")
@login_required
def logout():
    logout_user()
    return redirect("/")


"""
# Logout
@auth_blueprint.route("/logout")
@login_required
def logout():
    logout_user()
    flash("Logged out succesfully")
    return redirect(url_for("index"))
"""

"""
@auth_blueprint.route("/login")
def login():
    redirect_uri = url_for("auth.auth", _external=True)
    return keycloak.authorize_redirect(redirect_uri)
"""

"""
@auth_blueprint.route("/auth")
def auth():
    user = keycloak.userinfo()
    session["user"] = user
    return redirect(url_for("auth.home"))
"""
