from flask import render_template, url_for, flash, redirect, request
from ecommerceweb import app, db, bcrypt
from ecommerceweb.forms import RegistrationForm, LoginForm
from ecommerceweb.dbmodel import User
from flask_login import login_user, current_user, logout_user, login_required

@app.route("/")
@app.route("/home")
def home():
    return render_template('home.html')

@app.route("/product")
def product():
    return render_template('product_desc.html')

@app.route("/signup", methods=['GET', 'POST'])
def register():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        user = User(name=form.name.data, email=form.email.data, password=<PASSWORD>)
        db.session.add(user)
        db.session.commit()
        flash('Your account has been created! You can now log in', 'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)

@app.route("/login", methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password, form.password.data):
            login_user(user, remember=form.remember.data)
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(url_for('home'))
        else:
            flash('Login unsuccessful. Please check email and password', 'danger')
    return render_template('login.html', title='Login', form=form)

@app.route("/logout")
def logout():
    logout_user()
    return redirect(url_for('home'))

'''
@app.route("/cart")
@login_required
def cart():
    return render_template('cart.html', title='cart')
   
   
@app.route("/login", methods=['GET', 'POST'])
def login():
    return render_template('login.html', title='Login', form=form)
   ''' 
from flask import render_template, url_for, flash, redirect, request
from ecommerceweb import app, db, bcrypt
from ecommerceweb.forms import RegistrationForm, LoginForm
from ecommerceweb.dbmodel import User
from flask_login import login_user, current_user, logout_user, login_required

@app.route("/")
@app.route("/home")
def home():
    return render_template('home.html')

@app.route("/product")
def product():
    return render_template('product_desc.html')

@app.route("/signup", methods=['GET', 'POST'])
def register():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        user = User(name=form.name.data, email=form.email.data, password=<PASSWORD>)
        db.session.add(user)
        db.session.commit()
        flash('Your account has been created! You can now log in', 'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)

@app.route("/login", methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password, form.password.data):
            login_user(user, remember=form.remember.data)
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(url_for('home'))
        else:
            flash('Login unsuccessful. Please check email and password', 'danger')
    return render_template('login.html', title='Login', form=form)

@app.route("/logout")
def logout():
    logout_user()
    return redirect(url_for('home'))

'''
@app.route("/cart")
@login_required
def cart():
    return render_template('cart.html', title='cart')
   
   
@app.route("/login", methods=['GET', 'POST'])
def login():
    return render_template('login.html', title='Login', form=form)
   ''' 
@app.route("/cart") @login_required def cart():     return render_template('cart.html', title='cart')         @app.route("/login", methods=['GET', 'POST']) def login():     return render_template('login.html', title='Login', form=form)