"""
Authentication utilities and decorators
"""

from functools import wraps
from flask import session, flash, redirect, url_for, current_app


def login_required(f):
    """Decorator to require login for route access"""
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if 'logged_in' not in session:
            flash('Please log in to access this page.')
            return redirect(url_for('auth.login'))
        return f(*args, **kwargs)
    return decorated_function


def check_credentials(username, password):
    """Check if provided credentials are valid"""
    return (username == current_app.config['ADMIN_USERNAME'] and 
            password == current_app.config['ADMIN_PASSWORD'])


def login_user():
    """Log in the user by setting session"""
    session['logged_in'] = True


def logout_user():
    """Log out the user by clearing session"""
    session.pop('logged_in', None)


def is_authenticated():
    """Check if current user is authenticated"""
    return 'logged_in' in session