from fastapi import Request, Depends, HTTPException
from fastapi.responses import RedirectResponse


def get_current_user(request: Request) -> dict | None:
    return request.session.get("user")


def require_role(*roles: str):
    def dependency(request: Request, user: dict = Depends(get_current_user)):
        if not user:
            return RedirectResponse(url="/service/login", status_code=302)
        if roles and user["role"] not in roles:
            raise HTTPException(status_code=403, detail="Доступ запрещён")
        return user
    return dependency
