from flask import Blueprint, render_template, request, flash, redirect, url_for
from .models import User, Employee
from werkzeug.security import generate_password_hash, check_password_hash
from . import db
from flask_login import login_user, login_required, logout_user, current_user

# this file is a blueprint of our website
# it has all the routes and urls for the site

auth = Blueprint('auth', __name__)


# request will have the information about whatever data was sent when accessing the route
@auth.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        email = request.form.get('email')
        password = request.form.get('password')

        user = User.query.filter_by(email=email).first()
        print("user: ", user)
        if user:
            if check_password_hash(user.password, password):
                flash('Logged in successfully!', category='success')
                login_user(user, remember=True)
                return redirect(url_for('views.home'))
            else:
                flash('Incorrect password, try again.', category='error')
        else:
            flash('Email does not exist.', category='error')

    # return render_template("login.html", user=current_user)
    return render_template('login.html', user=current_user, text="test", check=False)


@auth.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('auth.login'))


@auth.route('/sign-up', methods=['GET', 'POST'])
def sign_up():
    if request.method == 'POST':
        email = request.form.get('email')
        firstname = request.form.get('firstName')
        lastname = request.form.get('lastName')
        department = request.form.get('department')
        password = request.form.get('password')
        confirmpassword = request.form.get('confirmpassword')
        user = User.query.filter_by(email=email).first()
        if user:
            flash('Email already exists.', category='error')
        elif len(email) < 4:
            flash('Email must be greater than 3 characters.', category='error')
        elif len(firstname) < 2:
            flash('First name must be greater than 1 character.', category='error')
        elif password != confirmpassword:
            flash('Passwords don\'t match.', category='error')
        elif len(password) < 7:
            flash('Password must be at least 7 characters.', category='error')
        else:
            new_user = User(email=email, firstName=firstname, lastName=lastname,
                            password=generate_password_hash(password, method='sha256'))
            db.session.add(new_user)
            db.session.flush()
            new_employee = Employee(employeeId=new_user.id, employeeName=(
                firstname+" "+lastname), employeeDepartment=department)
            db.session.add(new_employee)
            #print("id??", new_user.id)
            db.session.commit()

            login_user(new_user, remember=True)
            flash('Account created!', category='success')

            return redirect(url_for('views.home'))

    return render_template('signup.html', user=current_user)
