"""
app/admin/decorators.py
------------------------
A "decorator" is a small wrapper you place above a route function to run
some check before the route's own code runs.

@admin_required makes sure the person is (a) logged in and (b) has
role="admin" before letting them see an admin page. Any logged-in player
who tries to visit an admin URL directly gets redirected, not an error page.
"""

from functools import wraps
from flask import redirect, url_for, flash
from flask_login import current_user


def admin_required(view_func):
    @wraps(view_func)
    def wrapped(*args, **kwargs):
        if not current_user.is_authenticated:
            flash("Please log in.", "error")
            return redirect(url_for("auth.login"))
        if not current_user.is_admin():
            flash("That page is for club admins only.", "error")
            return redirect(url_for("player.dashboard"))
        return view_func(*args, **kwargs)
    return wrapped
