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

auth_bp = Blueprint('auth', __name__)

@auth_bp.route('/register', methods=['GET', 'POST'])
def register():
  if request.method == 'POST':
    email = request.form['email']
    password = request.form['password']
    if User.query.filter_by(email=email).first():
      flash("Email already registered", "danger")
      return redirect(url_for('auth.register'))
    user = User(email=email)
    user.set_password(password)
    db.session.add(user)
    db.session.commit()
    flash("Registered successfully!", "success")
    return redirect(url_for('auth.login'))
  return render_template('register.html')

@auth_bp.route('/login', methods=['GET', 'POST'])
def login():
  if request.method == 'POST':
    email = request.form['email']
    password = request.form['password']
    user = User.query.filter_by(email=email).first()
    if user and user.check_password(password):
      login_user(user)
      flash("Logged in successfully!", "success")
      next_page = request.args.get('next')
      return redirect(next_page or url_for('main.home'))
    flash("Invalid credentials", "danger")
  return render_template('login.html')

@auth_bp.route('/logout')
@login_required
def logout():
  logout_user()
  flash("Logged out successfully!", "info")
  return redirect(url_for('main.home'))
