"""Ajouter les permissions d'administration et d'intégration détaillées.
Revision ID: 9d28e9f0a1b2
Revises: 9c17d8e9f0a1
"""
from alembic import op
import sqlalchemy as sa
from app_new.core.authorization import GRANULAR_PERMISSION_CODES
revision = "9d28e9f0a1b2"
down_revision = "9c17d8e9f0a1"
branch_labels = None
depends_on = None
def upgrade():
    bind = op.get_bind()
    for code in sorted(GRANULAR_PERMISSION_CODES):
        bits = code.split(".", 1)
        module, action = bits[0], bits[1] if len(bits) > 1 else "manage"
        bind.execute(sa.text("INSERT INTO permissions (code,name,module,action,description,is_active) SELECT :code,:name,:module,:action,:description,1 FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM permissions WHERE code=:code)"), {"code": code, "name": f"{module.title()} — {action}", "module": module, "action": action, "description": "Action détaillée configurable"})
def downgrade():
    pass

