"""
Authentication routes
"""

from flask import Blueprint, render_template, request, redirect, url_for, flash
from app.utils.auth import check_credentials, login_user, logout_user

auth_bp = Blueprint('auth', __name__)


@auth_bp.route('/login', methods=['GET', 'POST'])
def login():
    """Login page and authentication"""
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        
        if check_credentials(username, password):
            login_user()
            flash('Successfully logged in!')
            return redirect(url_for('main.index'))
        else:
            flash('Invalid username or password.')
    
    return render_template('login.html')


@auth_bp.route('/logout')
def logout():
    """Logout user and redirect to main page"""
    logout_user()
    flash('You have been logged out.')
    return redirect(url_for('main.index'))