import logging

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from app.core.rbac import require_permission
from app.db.database import get_db
from app.models.user import User
from app.repositories.rbac_repository import RbacRepository
from app.schemas.rbac import PermissionResponse

logger = logging.getLogger(__name__)

router = APIRouter(
    prefix="/permissions",
    tags=["Permissions"],
)


@router.get("", response_model=list[PermissionResponse])
@router.get("/", response_model=list[PermissionResponse], include_in_schema=False)
def get_permissions(
    db: Session = Depends(get_db),
    current_user: User = Depends(require_permission("roles.read")),
):
    logger.info("permissions.list", extra={"actor_user_id": current_user.id})
    return RbacRepository.get_permissions(db)
